Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nix fmt to CI and clean up flake #58

Merged
merged 9 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/nix-lints.yml
Original file line number Diff line number Diff line change
@@ -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 -- .
3 changes: 1 addition & 2 deletions .github/workflows/rvoc-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/web-api-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
229 changes: 114 additions & 115 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
'';
};
};
}
Loading