From d44d2aa1a741898b6eac6bd46654b3f4069601f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Mon, 28 Oct 2024 21:50:33 +0100 Subject: [PATCH] feat: switch to crane Should provide better caching of the intermediate build artifacts. --- .cargo/config.toml | 2 +- default.nix | 43 --------------------- flake-modules/package.nix | 81 ++++++++++++++++++++++++++++++--------- flake.lock | 16 ++++++++ flake.nix | 5 ++- 5 files changed, 83 insertions(+), 64 deletions(-) delete mode 100644 default.nix diff --git a/.cargo/config.toml b/.cargo/config.toml index 60a7dd7..1ddbba2 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,2 @@ [build] -rustflags = ["-Clink-arg=-fuse-ld=mold", "-Zthreads=16"] +rustflags = ["-Clink-arg=-fuse-ld=mold"] diff --git a/default.nix b/default.nix deleted file mode 100644 index e224dd7..0000000 --- a/default.nix +++ /dev/null @@ -1,43 +0,0 @@ -{ lib, rustPlatform }: -let - readToml = path: builtins.fromTOML (builtins.readFile path); - cargoToml = readToml ./Cargo.toml; - pname = cargoToml.package.name; - inherit (cargoToml.package) version description; - src = lib.cleanSourceWith { - src = ./.; - name = "${pname}-source"; - # Adapted from - # no need to pull in crane for just this - filter = - orig_path: type: - let - path = toString orig_path; - base = baseNameOf path; - matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [ - # Rust sources - ".rs" - # TOML files are often used to configure cargo based tools (e.g. .cargo/config.toml) - ".toml" - # Snapshot tests - ".snap" - ]; - isCargoLock = base == "Cargo.lock"; - in - type == "directory" || matchesSuffix || isCargoLock; - }; -in -rustPlatform.buildRustPackage { - inherit pname version src; - cargoLock.lockFile = ./Cargo.lock; - useNextest = true; - NEXTEST_HIDE_PROGRESS_BAR = 1; - NEXTEST_FAILURE_OUTPUT = "immediate-final"; - - meta = { - inherit description; - license = lib.licenses.mit; - homepage = "https://github.com/jalil-salame/webnsupdate"; - mainProgram = "webnsupdate"; - }; -} diff --git a/flake-modules/package.nix b/flake-modules/package.nix index d4ff12a..f4d0cd0 100644 --- a/flake-modules/package.nix +++ b/flake-modules/package.nix @@ -1,24 +1,69 @@ +{ inputs, lib, ... }: { perSystem = { pkgs, ... }: - { - packages = - let - webnsupdate = pkgs.callPackage ../default.nix { }; - in - { - inherit webnsupdate; - default = webnsupdate; - cargo-update = pkgs.writeShellApplication { - name = "cargo-update-lockfile"; - runtimeInputs = with pkgs; [ - cargo - gnused - ]; - text = '' - CARGO_TERM_COLOR=never cargo update 2>&1 | sed '/crates.io index/d' | tee -a cargo_update.log - ''; - }; + let + craneLib = inputs.crane.mkLib pkgs; + src = craneLib.cleanCargoSource inputs.self; + + commonArgs = { + inherit src; + strictDeps = true; + + doCheck = false; # tests will be run in the `checks` derivation + NEXTEST_HIDE_PROGRESS_BAR = 1; + NEXTEST_FAILURE_OUTPUT = "immediate-final"; + + nativeBuildInputs = [ pkgs.mold ]; + + meta = { + license = lib.licenses.mit; + homepage = "https://github.com/jalil-salame/webnsupdate"; + mainProgram = "webnsupdate"; }; + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + webnsupdate = craneLib.buildPackage ( + lib.mergeAttrsList [ + commonArgs + { inherit cargoArtifacts; } + ] + ); + in + { + checks = { + clippy = craneLib.cargoClippy ( + lib.mergeAttrsList [ + commonArgs + { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + } + ] + ); + + nextest = craneLib.cargoNextest ( + lib.mergeAttrsList [ + commonArgs + { inherit cargoArtifacts; } + ] + ); + }; + + packages = { + inherit webnsupdate; + default = webnsupdate; + cargo-update = pkgs.writeShellApplication { + name = "cargo-update-lockfile"; + runtimeInputs = with pkgs; [ + cargo + gnused + ]; + text = '' + CARGO_TERM_COLOR=never cargo update 2>&1 | sed '/crates.io index/d' | tee -a cargo_update.log + ''; + }; + }; }; } diff --git a/flake.lock b/flake.lock index e5a0f2a..2987e17 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1730060262, + "narHash": "sha256-RMgSVkZ9H03sxC+Vh4jxtLTCzSjPq18UWpiM0gq6shQ=", + "owner": "ipetkov", + "repo": "crane", + "rev": "498d9f122c413ee1154e8131ace5a35a80d8fa76", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": [ @@ -37,6 +52,7 @@ }, "root": { "inputs": { + "crane": "crane", "flake-parts": "flake-parts", "nixpkgs": "nixpkgs", "systems": "systems", diff --git a/flake.nix b/flake.nix index 3fb5460..3e7f04f 100644 --- a/flake.nix +++ b/flake.nix @@ -1,12 +1,13 @@ { description = "An http server that calls nsupdate internally"; inputs = { - nixpkgs.url = "nixpkgs/nixos-unstable"; - systems.url = "github:nix-systems/default"; + crane.url = "github:ipetkov/crane"; flake-parts = { url = "github:hercules-ci/flake-parts"; inputs.nixpkgs-lib.follows = "nixpkgs"; }; + nixpkgs.url = "nixpkgs/nixos-unstable"; + systems.url = "github:nix-systems/default"; treefmt-nix = { url = "github:numtide/treefmt-nix"; inputs.nixpkgs.follows = "nixpkgs";