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

244
flake.nix
View File

@ -39,32 +39,22 @@
... ...
}: }:
flake-utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system: let
pname = "Heartwood"; lib = nixpkgs.lib;
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
overlays = [(import rust-overlay)]; overlays = [(import rust-overlay)];
}; };
inherit (pkgs) lib;
rustToolChain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain; rustToolChain = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolChain; craneLib = (crane.mkLib pkgs).overrideToolchain rustToolChain;
srcFilters = path: type: srcFilters = path: type:
# Allow sql schemas builtins.any (suffix: lib.hasSuffix suffix path) [
(lib.hasSuffix "\.sql" path) ".sql" # schemas
|| ".diff" # testing
# Allow diff files for testing purposes ".md" # testing
(lib.hasSuffix "\.diff" path) ".adoc" # man pages
|| ]
# 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)
|| ||
# Default filter from crane (allow .rs files) # Default filter from crane (allow .rs files)
(craneLib.filterCargoSources path type); (craneLib.filterCargoSources path type);
@ -74,60 +64,45 @@
filter = srcFilters; filter = srcFilters;
}; };
# Common arguments can be set here to avoid repeating them later basicArgs = {
commonArgs = {
inherit pname;
inherit src; inherit src;
pname = "Heartwood";
strictDeps = true; 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 # Build *just* the cargo dependencies, so we can reuse
# all of that work (e.g. via cachix) when running in CI # all of that work (e.g. via cachix) when running in CI
cargoArtifacts = cargoArtifacts = craneLib.buildDepsOnly basicArgs;
craneLib.buildDepsOnly commonArgs;
# Build the listed .adoc files as man pages to the package. # Common arguments can be set here to avoid repeating them later
buildManPages = pages: { commonArgs =
nativeBuildInputs = [ pkgs.asciidoctor ]; basicArgs
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
// { // {
inherit (craneLib.crateNameFromCargoToml {cargoToml = ./radicle/Cargo.toml;});
doCheck = false;
inherit cargoArtifacts; inherit cargoArtifacts;
} // (buildManPages [
"git-remote-rad.1.adoc" nativeBuildInputs = with pkgs;
"rad.1.adoc" [
"radicle-node.1.adoc" git
"rad-patch.1.adoc" # Add additional build inputs here
"rad-id.1.adoc" ]
])); ++ 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 { in {
# Formatter # Formatter
formatter = pkgs.alejandra; formatter = pkgs.alejandra;
@ -135,7 +110,7 @@
# Set of checks that are run: `nix flake check` # Set of checks that are run: `nix flake check`
checks = { checks = {
# Build the crate as part of `nix flake check` for convenience # 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, # Run clippy (and deny all warnings) on the crate source,
# again, reusing the dependency artifacts from above. # again, reusing the dependency artifacts from above.
@ -145,37 +120,20 @@
# prevent downstream consumers from building our crate by itself. # prevent downstream consumers from building our crate by itself.
clippy = craneLib.cargoClippy (commonArgs clippy = craneLib.cargoClippy (commonArgs
// { // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings"; cargoClippyExtraArgs = "--all-targets -- --deny warnings";
}); });
doc = craneLib.cargoDoc (commonArgs doc = craneLib.cargoDoc commonArgs;
// { deny = craneLib.cargoDeny commonArgs;
inherit cargoArtifacts; fmt = craneLib.cargoFmt basicArgs;
});
# Check formatting audit = craneLib.cargoAudit {
fmt = craneLib.cargoFmt { inherit src advisory-db;
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;
}; };
# Run tests with cargo-nextest # Run tests with cargo-nextest
nextest = craneLib.cargoNextest (commonArgs nextest = craneLib.cargoNextest (commonArgs
// { // {
inherit cargoArtifacts;
partitions = 1; partitions = 1;
partitionType = "count"; partitionType = "count";
nativeBuildInputs = [ nativeBuildInputs = [
@ -186,55 +144,73 @@
]; ];
# Ensure dev is used since we rely on env variables being # Ensure dev is used since we rely on env variables being
# set in tests. # set in tests.
buildPhase = '' env.CARGO_PROFILE = "dev";
export CARGO_PROFILE=dev;
'';
}); });
}; };
packages = { packages = let
default = radicle; crate = {
radicle-full = pkgs.buildEnv { name,
name = "radicle-full"; pages ? [],
paths = with self.packages.${system}; [ }:
default craneLib.buildPackage (commonArgs
radicle-httpd // {
]; 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 { apps.default = flake-utils.lib.mkApp {
drv = radicle; drv = self.packages.${system}.radicle;
}; };
apps.radicle-full = flake-utils.lib.mkApp { apps.radicle-full = flake-utils.lib.mkApp {
@ -264,12 +240,12 @@
devShells.default = craneLib.devShell { devShells.default = craneLib.devShell {
# Extra inputs can be added here; cargo and rustc are provided by default. # Extra inputs can be added here; cargo and rustc are provided by default.
packages = [ packages = with pkgs; [
pkgs.cargo-watch cargo-watch
pkgs.cargo-nextest cargo-nextest
pkgs.ripgrep ripgrep
pkgs.rust-analyzer rust-analyzer
pkgs.sqlite sqlite
]; ];
}; };
}); });