Skip to content

Commit

Permalink
nix: refactor flake (#65)
Browse files Browse the repository at this point in the history
* nix: drop flake-utils

* nix: drop rust-overlay

* nix: don't re-instantiate nixpkgs
Previously, applying the overlay to our instance of nixpkgs added about
~100mb of memory to each evaluation, along with a few extra seconds to
evaluate (https://zimbatm.com/notes/1000-instances-of-nixpkgs)

* nix: use nix-filter to filter source
Avoids NixOS/nix#9428 (path coercion like `"${./.}"` causes files to be added
to the store twice)
  • Loading branch information
getchoo authored Aug 19, 2024
1 parent 1e11638 commit 82259e7
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 174 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ jobs:

strategy:
matrix:
arch:
- x86_64
- aarch64
target:
- "x86_64-unknown-linux-musl"
- "aarch64-unknown-linux-musl"

runs-on: ubuntu-latest

Expand All @@ -97,7 +97,7 @@ jobs:
uses: DeterminateSystems/magic-nix-cache-action@main

- name: Build
run: nix build --fallback --print-build-logs '.#nrr-static-${{ matrix.arch }}'
run: nix build --fallback --print-build-logs '.#nrr-static-${{ matrix.target }}'

- name: Generate build provenance attestations
uses: actions/attest-build-provenance@v1
Expand All @@ -109,5 +109,5 @@ jobs:
uses: actions/upload-artifact@v4
with:
if-no-files-found: "error"
name: nrr-${{ matrix.arch }}-unknown-linux-musl
name: nrr-${{ matrix.target }}
path: ./result/bin/nrr
55 changes: 8 additions & 47 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

196 changes: 108 additions & 88 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,55 @@

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
nix-filter.url = "github:numtide/nix-filter";
};

outputs =
{
self,
nixpkgs,
flake-utils,
...
}@inputs:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
config = { };
overlays = [
inputs.rust-overlay.overlays.default
self.overlays.default
];
};

inherit (pkgs) lib;

mkFlakeCheck =
{
name,
nativeBuildInputs ? [ ],
command,
}:
pkgs.stdenv.mkDerivation {
name = "check-${name}";
inherit nativeBuildInputs;
inherit (self.packages.${system}.nrr) src cargoDeps;

buildPhase = ''
${command}
touch "$out"
'';

doCheck = false;
dontInstall = true;
dontFixup = true;
};
in
{
checks = {
nix-filter,
}:
let
inherit (nixpkgs) lib;
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];

forAllSystems = lib.genAttrs systems;
nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system});
in
{
checks = forAllSystems (
system:
let
pkgs = nixpkgsFor.${system};

mkFlakeCheck =
{
name,
nativeBuildInputs ? [ ],
command,
}:
pkgs.stdenv.mkDerivation {
name = "check-${name}";
inherit nativeBuildInputs;
inherit (self.packages.${system}.nrr) src cargoDeps;

buildPhase = ''
${command}
touch "$out"
'';

doCheck = false;
dontInstall = true;
dontFixup = true;
};
in
{
nixfmt = mkFlakeCheck {
name = "nixfmt";
nativeBuildInputs = with pkgs; [ nixfmt-rfc-style ];
Expand All @@ -68,15 +65,18 @@

rustfmt = mkFlakeCheck {
name = "rustfmt";

nativeBuildInputs = with pkgs; [
cargo
rustfmt
];

command = "cargo fmt --check";
};

clippy = mkFlakeCheck {
name = "clippy";

nativeBuildInputs = with pkgs; [
rustPlatform.cargoSetupHook
cargo
Expand All @@ -85,54 +85,74 @@
clippy-sarif
sarif-fmt
];

command = ''
cargo clippy --all-features --all-targets --tests \
--offline --message-format=json \
| clippy-sarif | tee $out | sarif-fmt
'';
};
};

devShells.default = pkgs.mkShell {
packages = with pkgs; [
rustc
cargo
rustfmt
clippy
rust-analyzer

git-cliff # changelog generator
taplo # TOML toolkit

cargo-audit
cargo-bloat
cargo-expand

libiconv
];

__structuredAttrs = true;
env = {
RUST_BACKTRACE = 1;
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
}
);

devShells = forAllSystems (
system:
let
pkgs = nixpkgsFor.${system};
in
{
default = pkgs.mkShell {
packages = with pkgs; [
rustfmt
clippy
rust-analyzer

git-cliff # changelog generator
taplo # TOML toolkit

cargo-audit
cargo-bloat
cargo-expand

libiconv
];

inputsFrom = [ self.packages.${system}.nrr ];

__structuredAttrs = true;
env = {
RUST_BACKTRACE = 1;
RUST_SRC_PATH = toString pkgs.rustPlatform.rustLibSrc;
};
};
};

packages =
{
inherit (pkgs) nrr;
default = pkgs.nrr;
}
// (lib.attrsets.mapAttrs' (
name: value: lib.nameValuePair "check-${name}" value
) self.checks.${system});

legacyPackages = import ./nix/static.nix pkgs;

formatter = pkgs.nixfmt-rfc-style;
}
)
// {
overlays.default = _: prev: { nrr = prev.callPackage ./nix/package.nix { }; };
}
);

packages = forAllSystems (
system:
let
pkgs = nixpkgsFor.${system};

# re-use our overlay to call packages
packages = self.overlays.default null pkgs;
in
{
inherit (packages) nrr;
default = packages.nrr;
}
// (lib.attrsets.mapAttrs' (
name: value: lib.nameValuePair "check-${name}" value
) self.checks.${system})
);

legacyPackages = forAllSystems (
system: nixpkgsFor.${system}.callPackage ./nix/static.nix { inherit nix-filter self; }
);

formatter = forAllSystems (system: nixpkgsFor.${system}.nixfmt-rfc-style);

overlays.default = _: prev: {
nrr = prev.callPackage ./nix/package.nix { inherit nix-filter self; };
};
};
}
17 changes: 10 additions & 7 deletions nix/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
stdenv,
rustPlatform,
darwin,
nix-filter,
pkg-config,
self,
enableLTO ? true,
enableOptimizeSize ? false,
nrxAlias ? true,
}:

rustPlatform.buildRustPackage rec {
pname = passthru.cargoToml.package.name;
inherit (passthru.cargoToml.package) version;

__structuredAttrs = true;
strictDeps = true;

src = lib.fileset.toSource {
root = ../.;
fileset = lib.fileset.unions [
../src
../tests
../Cargo.lock
../Cargo.toml
src = nix-filter.lib.filter {
root = self;
include = [
"src"
"tests"
"Cargo.lock"
"Cargo.toml"
];
};

Expand Down
Loading

0 comments on commit 82259e7

Please sign in to comment.