nix: Polish `flake.nix`

* Remove empty `inherit`s

  These were not doing anything.

      { inherit (f) x; }

  is equivalent to

      { x = f.x; }

  but

      { inherit (f); }

  is equivalent to

      {}

  since there is no attribute name given.

* Refactor `srcFilters` to use `any`

  That's more lighweight and easier to extend.

* Use `installShellFiles` for `buildManPages`
* Produce a more stable version
* Move package `radicle` next to the others

  It's nothing special!

* Refactor Flake

  More code reuse, and a little more direct, basically.

* Set environment variable on derivation
* Compress even more by removing special crate
* Use `with pkgs` before lists of packages
* Use system-independent `lib`
* Clean up
* Update `flake.lock`
* Enable checks `audit`, `deny`
This commit is contained in:
Lorenz Leutgeb 2024-05-09 15:45:52 +02:00 committed by cloudhead
parent d839418c56
commit b2992814b6
No known key found for this signature in database
2 changed files with 125 additions and 149 deletions

View File

@ -3,11 +3,11 @@
"advisory-db": {
"flake": false,
"locked": {
"lastModified": 1701193254,
"narHash": "sha256-Hr7efA3GjwqBkGYKmd3XmGckdPQikbcCmOrq7fmTp3A=",
"lastModified": 1714183630,
"narHash": "sha256-1BVft7ggSN2XXFeXQjazU3jN9wVECd9qp2mZx/8GDMk=",
"owner": "rustsec",
"repo": "advisory-db",
"rev": "43af5fef0591531a72ebb86c5f1c623ee95c62fe",
"rev": "35e7459a331d3e0c585e56dabd03006b9b354088",
"type": "github"
},
"original": {
@ -23,11 +23,11 @@
]
},
"locked": {
"lastModified": 1701622587,
"narHash": "sha256-o3XhxCCyrUHZ0tlta2W7/MuXzy+n0+BUt3rKFK3DIK4=",
"lastModified": 1715274763,
"narHash": "sha256-3Iv1PGHJn9sV3HO4FlOVaaztOxa9uGLfOmUWrH7v7+A=",
"owner": "ipetkov",
"repo": "crane",
"rev": "c09d2cbe84cc2adfe1943cb2a0b55a71c835ca9a",
"rev": "27025ab71bdca30e7ed0a16c88fd74c5970fc7f5",
"type": "github"
},
"original": {
@ -41,11 +41,11 @@
"systems": "systems"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
@ -56,11 +56,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1701961404,
"narHash": "sha256-ieWHyh6kJtabQYUam/dXXi22MgcgLQvl+f2x/95n+us=",
"lastModified": 1715336739,
"narHash": "sha256-JpcAHWgeVo6z3uUg+xzaeZSLn7MlcZMo9y6QQ7+Ue6A=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8fef9eee026f0d95c06b5880ef9c1af0f643aadf",
"rev": "baefb35c627d6bc698fd376774506f5f616bc1f3",
"type": "github"
},
"original": {
@ -89,11 +89,11 @@
]
},
"locked": {
"lastModified": 1711419061,
"narHash": "sha256-+5M/czgYGqs/jKmi8bvYC+JUYboUKNTfkRiesXopeXQ=",
"lastModified": 1715307487,
"narHash": "sha256-yuDAys3JuJmhQUQGMMsl3BDQNZUYZDw0eA71OVh9FeY=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "4c11d2f698ff1149f76b69e72852d5d75f492d0c",
"rev": "ec7a7caf50877bc32988c82653d6b3e6952a8c3f",
"type": "github"
},
"original": {

244
flake.nix
View File

@ -39,32 +39,22 @@
...
}:
flake-utils.lib.eachDefaultSystem (system: let
pname = "Heartwood";
lib = nixpkgs.lib;
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
inherit (pkgs) lib;
rustToolChain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolChain;
srcFilters = path: type:
# Allow sql schemas
(lib.hasSuffix "\.sql" path)
||
# Allow diff files for testing purposes
(lib.hasSuffix "\.diff" path)
||
# Allow md files for testing purposes
(lib.hasSuffix "\.md" path)
||
# Allow adoc files
(lib.hasSuffix "\.adoc" path)
||
# Allow man page build script
(lib.hasSuffix "build-man-pages\.sh" path)
builtins.any (suffix: lib.hasSuffix suffix path) [
".sql" # schemas
".diff" # testing
".md" # testing
".adoc" # man pages
]
||
# Default filter from crane (allow .rs files)
(craneLib.filterCargoSources path type);
@ -74,60 +64,45 @@
filter = srcFilters;
};
# Common arguments can be set here to avoid repeating them later
commonArgs = {
inherit pname;
basicArgs = {
inherit src;
pname = "Heartwood";
strictDeps = true;
buildInputs =
[
pkgs.git
# Add additional build inputs here
]
++ lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
];
preBuild = lib.optionalString (self.shortRev or null != null) ''
export GIT_HEAD=${self.shortRev}
'';
};
# Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI
cargoArtifacts =
craneLib.buildDepsOnly commonArgs;
cargoArtifacts = craneLib.buildDepsOnly basicArgs;
# Build the listed .adoc files as man pages to the package.
buildManPages = pages: {
nativeBuildInputs = [ pkgs.asciidoctor ];
postInstall = ''
for f in ${lib.escapeShellArgs pages} ; do
cat=''${f%.adoc}
cat=''${cat##*.}
[ -d "$out/share/man/man$cat" ] || mkdir -p "$out/share/man/man$cat"
scripts/build-man-pages.sh "$out/share/man/man$cat" $f
done
'';
outputs = [ "out" "man" ];
};
# Build the actual crate itself, reusing the dependency
# artifacts from above.
radicle = craneLib.buildPackage (commonArgs
# Common arguments can be set here to avoid repeating them later
commonArgs =
basicArgs
// {
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle/Cargo.toml;});
doCheck = false;
inherit cargoArtifacts;
} // (buildManPages [
"git-remote-rad.1.adoc"
"rad.1.adoc"
"radicle-node.1.adoc"
"rad-patch.1.adoc"
"rad-id.1.adoc"
]));
nativeBuildInputs = with pkgs;
[
git
# Add additional build inputs here
]
++ lib.optionals pkgs.stdenv.isDarwin (with pkgs; [
# Additional darwin specific inputs can be set here
libiconv
darwin.apple_sdk.frameworks.Security
]);
env =
{
RADICLE_VERSION = "nix-" + (self.shortRev or self.dirtyShortRev or "unknown");
}
// (
if self ? rev || self ? dirtyRev
then {
GIT_HEAD = self.rev or self.dirtyRev;
}
else {}
);
};
in {
# Formatter
formatter = pkgs.alejandra;
@ -135,7 +110,7 @@
# Set of checks that are run: `nix flake check`
checks = {
# Build the crate as part of `nix flake check` for convenience
inherit radicle;
inherit (self.packages.${system}) radicle;
# Run clippy (and deny all warnings) on the crate source,
# again, reusing the dependency artifacts from above.
@ -145,37 +120,20 @@
# prevent downstream consumers from building our crate by itself.
clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
doc = craneLib.cargoDoc (commonArgs
// {
inherit cargoArtifacts;
});
doc = craneLib.cargoDoc commonArgs;
deny = craneLib.cargoDeny commonArgs;
fmt = craneLib.cargoFmt basicArgs;
# Check formatting
fmt = craneLib.cargoFmt {
inherit pname;
inherit src;
};
# TODO: audits are failing so skip this check for now
# Audit dependencies
# audit = craneLib.cargoAudit {
# inherit src advisory-db;
# };
# Audit licenses
deny = craneLib.cargoDeny {
inherit pname;
inherit src;
audit = craneLib.cargoAudit {
inherit src advisory-db;
};
# Run tests with cargo-nextest
nextest = craneLib.cargoNextest (commonArgs
// {
inherit cargoArtifacts;
partitions = 1;
partitionType = "count";
nativeBuildInputs = [
@ -186,55 +144,73 @@
];
# Ensure dev is used since we rely on env variables being
# set in tests.
buildPhase = ''
export CARGO_PROFILE=dev;
'';
env.CARGO_PROFILE = "dev";
});
};
packages = {
default = radicle;
radicle-full = pkgs.buildEnv {
name = "radicle-full";
paths = with self.packages.${system}; [
default
radicle-httpd
];
packages = let
crate = {
name,
pages ? [],
}:
craneLib.buildPackage (commonArgs
// {
inherit (craneLib.crateNameFromCargoToml {cargoToml = src + "/" + name + "/Cargo.toml";}) pname version;
cargoExtraArgs = "-p ${name}";
doCheck = false;
nativeBuildInputs = with pkgs; [asciidoctor installShellFiles];
postInstall = ''
for page in ${lib.escapeShellArgs pages}; do
asciidoctor -d manpage -b manpage $page
installManPage ''${page::-5}
done
'';
});
crates = builtins.listToAttrs (map
({name, ...} @ package: lib.nameValuePair name (crate package))
[
{
name = "radicle-httpd";
pages = ["radicle-httpd.1.adoc"];
}
{
name = "radicle-cli";
pages = [
"rad.1.adoc"
"rad-id.1.adoc"
"rad-patch.1.adoc"
];
}
{
name = "radicle-remote-helper";
pages = ["git-remote-rad.1.adoc"];
}
{
name = "radicle-node";
pages = ["radicle-node.1.adoc"];
}
]);
in
crates
// rec {
default = radicle;
radicle = pkgs.buildEnv {
name = "radicle";
paths = with crates; [
radicle-cli
radicle-node
radicle-remote-helper
];
};
radicle-full = pkgs.buildEnv {
name = "radicle-full";
paths = builtins.attrValues crates;
};
};
radicle-remote-helper = craneLib.buildPackage (commonArgs
// {
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle-remote-helper/Cargo.toml;});
inherit cargoArtifacts;
cargoBuildCommand = "cargo build --release -p radicle-remote-helper";
doCheck = false;
});
radicle-cli = craneLib.buildPackage (commonArgs
// {
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle-cli/Cargo.toml;});
inherit cargoArtifacts;
cargoBuildCommand = "cargo build --release -p radicle-cli";
doCheck = false;
});
radicle-node = craneLib.buildPackage (commonArgs
// {
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle-node/Cargo.toml;});
inherit cargoArtifacts;
cargoBuildCommand = "cargo build --release -p radicle-node";
doCheck = false;
});
radicle-httpd = craneLib.buildPackage (commonArgs
// {
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle-httpd/Cargo.toml;});
inherit cargoArtifacts;
cargoBuildCommand = "cargo build --release -p radicle-httpd";
doCheck = false;
} // (buildManPages [
"radicle-httpd.1.adoc"
]));
};
apps.default = flake-utils.lib.mkApp {
drv = radicle;
drv = self.packages.${system}.radicle;
};
apps.radicle-full = flake-utils.lib.mkApp {
@ -264,12 +240,12 @@
devShells.default = craneLib.devShell {
# Extra inputs can be added here; cargo and rustc are provided by default.
packages = [
pkgs.cargo-watch
pkgs.cargo-nextest
pkgs.ripgrep
pkgs.rust-analyzer
pkgs.sqlite
packages = with pkgs; [
cargo-watch
cargo-nextest
ripgrep
rust-analyzer
sqlite
];
};
});