diff --git a/.github/workflows/nix-lints.yml b/.github/workflows/nix-lints.yml new file mode 100644 index 0000000..613538a --- /dev/null +++ b/.github/workflows/nix-lints.yml @@ -0,0 +1,55 @@ +name: Nix Lints + +on: + push: + paths: + # Run if workflow changes + - '.github/workflows/nix-lints.yml' + # Run on changed flake + - 'flake.nix' + - 'flake.lock' + branches: + - main + pull_request: + branches: main + # Run manually + workflow_dispatch: + +jobs: + nix_fmt: + name: Nix format + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install nix + uses: cachix/install-nix-action@v22 + + - name: Cache /nix/store + uses: actions/cache@v3 + with: + path: /nix/store + key: ${{ runner.os }}-${{ hashFiles('flake.*') }}-${{ hashFiles('.github/workflows/web-api-ci.yml') }} + + - name: Check nix formatting + run: nix fmt --accept-flake-config -- --check . + + nix_dead_code: + name: Nix dead code + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install nix + uses: cachix/install-nix-action@v22 + + - name: Cache /nix/store + uses: actions/cache@v3 + with: + path: /nix/store + key: ${{ runner.os }}-${{ hashFiles('flake.*') }}-${{ hashFiles('.github/workflows/web-api-ci.yml') }} + + - name: Check for dead nix code + run: nix run github:astro/deadnix -- . \ No newline at end of file diff --git a/.github/workflows/rvoc-backend.yml b/.github/workflows/rvoc-backend.yml index 038426a..3862d69 100644 --- a/.github/workflows/rvoc-backend.yml +++ b/.github/workflows/rvoc-backend.yml @@ -16,8 +16,7 @@ on: branches: main pull_request: branches: main - # Sometimes the rules above don't match even though they should. - # This allows us to run the workflow manually anyways. + # Run manually workflow_dispatch: env: diff --git a/.github/workflows/web-api-ci.yml b/.github/workflows/web-api-ci.yml index 410b1b4..f2e1ec3 100644 --- a/.github/workflows/web-api-ci.yml +++ b/.github/workflows/web-api-ci.yml @@ -18,17 +18,14 @@ on: - 'flake.lock' branches: - main - # We are creating this workflow on this branch, hence we enable it here. - # This can be deleted once the branch is removed. - - 42-add-user-account-creation pull_request: branches: main - # Sometimes the rules above don't match even though they should. - # This allows us to run the workflow manually anyways. + # Run manually workflow_dispatch: jobs: run_integration_tests: + name: Backend integration tests runs-on: ubuntu-latest steps: - name: Checkout diff --git a/flake.nix b/flake.nix index 4ac2b51..bc741ea 100644 --- a/flake.nix +++ b/flake.nix @@ -18,120 +18,119 @@ }; }; }; - outputs = {self, nixpkgs, flake-utils, rust-overlay, crane}: - flake-utils.lib.eachDefaultSystem - (system: - let - system = "x86_64-linux"; - overlays = [(import rust-overlay)]; - pkgs = import nixpkgs { - inherit system overlays; - }; - inherit (pkgs) lib; - rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; - craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; - src = lib.cleanSourceWith { - src = ./.; # The original, unfiltered source - filter = path: type: - # Allow sql files for migrations - (lib.hasSuffix "\.sql" path) || - # Default filter from crane (allow .rs files) - (craneLib.filterCargoSources path type) - ; - }; - nativeBuildInputs = with pkgs; [rustToolchain pkg-config]; - buildInputs = with pkgs; [rustToolchain openssl postgresql_15.lib]; - developmentTools = with pkgs; [(diesel-cli.override {sqliteSupport = false; mysqlSupport = false;}) postgresql cargo]; - commonArgs = { - inherit src buildInputs nativeBuildInputs; - installCargoArtifactsMode = "use-zstd"; - strictDeps = true; - }; - integrationTestsArtifacts = craneLib.buildDepsOnly(commonArgs // { - cargoBuildCommand = "cargo build --profile dev"; - cargoExtraArgs = "--bin integration-tests"; - doCheck = false; - pname = "integration-tests"; - }); - integrationTestsBinary = craneLib.buildPackage(commonArgs // { - cargoArtifacts = integrationTestsArtifacts; - cargoBuildCommand = "cargo build --profile dev"; - cargoExtraArgs = "--bin integration-tests"; - doCheck = false; - pname = "integration-tests"; - }); - cargoDebugArtifacts = craneLib.buildDepsOnly(commonArgs // { - cargoBuildCommand = "cargo build --profile dev"; - cargoExtraArgs = "--bin rvoc-backend"; - doCheck = false; - pname = "rvoc-backend"; - }); - debugBinary = craneLib.buildPackage(commonArgs // { - cargoArtifacts = cargoDebugArtifacts; - cargoBuildCommand = "cargo build --profile dev"; - cargoExtraArgs = "--bin rvoc-backend"; - doCheck = false; - pname = "rvoc-backend"; - }); - cargoArtifacts = craneLib.buildDepsOnly(commonArgs // { - cargoBuildCommand = "cargo build --profile release"; - cargoExtraArgs = "--bin rvoc-backend"; - doCheck = false; - pname = "rvoc-backend"; - }); - binary = craneLib.buildPackage(commonArgs // { - cargoArtifacts = cargoArtifacts; - cargoBuildCommand = "cargo build --profile release"; - cargoExtraArgs = "--bin rvoc-backend"; - pname = "rvoc-backend"; - }); - dockerImage = pkgs.dockerTools.streamLayeredImage { - name = "rvoc-backend"; - tag = "latest"; - contents = [binary pkgs.cacert]; - config = { - Cmd = ["${binary}/bin/rvoc-backend"]; - }; - }; - debugDockerImage = pkgs.dockerTools.streamLayeredImage { - name = "rvoc-backend"; - tag = "latest"; - contents = [debugBinary pkgs.cacert]; - config = { - Cmd = ["${debugBinary}/bin/rvoc-backend"]; - }; - }; - in - with pkgs; - { - packages = { - inherit binary debugBinary integrationTestsBinary dockerImage debugDockerImage; - default = binary; - }; - devShells.default = mkShell { - inputsFrom = [binary]; - buildInputs = with pkgs; [dive wget]; - packages = developmentTools; - shellHook = '' - export PGDATA=$PWD/backend/data/postgres_dev_data - export PGHOST=$PWD/backend/data/postgres_dev - export LOG_PATH=$PWD/backend/data/postgres_dev/LOG - export PGDATABASE=rvoc_dev - export POSTGRES_RVOC_URL="postgresql://''${USER}@/''${PGDATABASE}?host=$PGHOST" - export DATABASE_URL=$POSTGRES_RVOC_URL - if [ ! -d $PGHOST ]; then - mkdir -p $PGHOST - fi - if [ ! -d $PGDATA ]; then - echo 'Initializing postgresql database...' - initdb $PGDATA --auth=trust >/dev/null - fi - echo "Starting postgres" - pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST" - echo "Shell hook finished" - ''; - }; - } + outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }: + let + system = "x86_64-linux"; + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + inherit (pkgs) lib; + rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + src = lib.cleanSourceWith { + src = ./.; # The original, unfiltered source + filter = path: type: + # Allow sql files for migrations + (lib.hasSuffix "\.sql" path) || + # Default filter from crane (allow .rs files) + (craneLib.filterCargoSources path type) + ; + }; + nativeBuildInputs = with pkgs; [ rustToolchain pkg-config ]; + buildInputs = with pkgs; [ rustToolchain openssl postgresql_15.lib ]; + developmentTools = with pkgs; [ (diesel-cli.override { sqliteSupport = false; mysqlSupport = false; }) postgresql cargo ]; + commonArgs = { + inherit src buildInputs nativeBuildInputs; + installCargoArtifactsMode = "use-zstd"; + strictDeps = true; + }; + integrationTestsArtifacts = craneLib.buildDepsOnly (commonArgs // { + cargoBuildCommand = "cargo build --profile dev"; + cargoExtraArgs = "--bin integration-tests"; + doCheck = false; + pname = "integration-tests"; + }); + integrationTestsBinary = craneLib.buildPackage (commonArgs // { + cargoArtifacts = integrationTestsArtifacts; + cargoBuildCommand = "cargo build --profile dev"; + cargoExtraArgs = "--bin integration-tests"; + doCheck = false; + pname = "integration-tests"; + }); + cargoDebugArtifacts = craneLib.buildDepsOnly (commonArgs // { + cargoBuildCommand = "cargo build --profile dev"; + cargoExtraArgs = "--bin rvoc-backend"; + doCheck = false; + pname = "rvoc-backend"; + }); + debugBinary = craneLib.buildPackage (commonArgs // { + cargoArtifacts = cargoDebugArtifacts; + cargoBuildCommand = "cargo build --profile dev"; + cargoExtraArgs = "--bin rvoc-backend"; + doCheck = false; + pname = "rvoc-backend"; + }); + cargoArtifacts = craneLib.buildDepsOnly (commonArgs // { + cargoBuildCommand = "cargo build --profile release"; + cargoExtraArgs = "--bin rvoc-backend"; + doCheck = false; + pname = "rvoc-backend"; + }); + binary = craneLib.buildPackage (commonArgs // { + cargoArtifacts = cargoArtifacts; + cargoBuildCommand = "cargo build --profile release"; + cargoExtraArgs = "--bin rvoc-backend"; + pname = "rvoc-backend"; + }); + dockerImage = pkgs.dockerTools.streamLayeredImage { + name = "rvoc-backend"; + tag = "latest"; + contents = [ binary pkgs.cacert ]; + config = { + Cmd = [ "${binary}/bin/rvoc-backend" ]; + }; + }; + debugDockerImage = pkgs.dockerTools.streamLayeredImage { + name = "rvoc-backend"; + tag = "latest"; + contents = [ debugBinary pkgs.cacert ]; + config = { + Cmd = [ "${debugBinary}/bin/rvoc-backend" ]; + }; + }; + in + with pkgs; + { + packages.${system} = { + inherit binary debugBinary integrationTestsBinary dockerImage debugDockerImage; + default = binary; + }; + + formatter.${system} = pkgs.nixpkgs-fmt; - ); + devShells.${system}.default = mkShell { + inputsFrom = [ binary ]; + buildInputs = with pkgs; [ dive wget ]; + packages = developmentTools; + shellHook = '' + export PGDATA=$PWD/backend/data/postgres_dev_data + export PGHOST=$PWD/backend/data/postgres_dev + export LOG_PATH=$PWD/backend/data/postgres_dev/LOG + export PGDATABASE=rvoc_dev + export POSTGRES_RVOC_URL="postgresql://''${USER}@/''${PGDATABASE}?host=$PGHOST" + export DATABASE_URL=$POSTGRES_RVOC_URL + if [ ! -d $PGHOST ]; then + mkdir -p $PGHOST + fi + if [ ! -d $PGDATA ]; then + echo 'Initializing postgresql database...' + initdb $PGDATA --auth=trust >/dev/null + fi + echo "Starting postgres" + pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST" + echo "Shell hook finished" + ''; + }; + }; }