From 864ece025e58be767d29430ac4d4a4ac293f0a2a Mon Sep 17 00:00:00 2001 From: Joseph Goulden Date: Sun, 29 Sep 2024 15:01:08 +0100 Subject: [PATCH] build: Add nix derivation for building cargo workspace --- flake.lock | 76 +++++++++++++++++++++++++++++++++++-- flake.nix | 29 ++++++++++---- nix/checks/flake-module.nix | 19 ++++++++++ nix/pkgs/default.nix | 4 +- nix/pkgs/flake-module.nix | 13 +++++++ nix/pkgs/rust.nix | 19 ++++++++++ nix/shells.nix | 19 ++++++++++ nix/treefmt.nix | 11 ++++++ 8 files changed, 176 insertions(+), 14 deletions(-) create mode 100644 nix/checks/flake-module.nix create mode 100644 nix/pkgs/flake-module.nix create mode 100644 nix/pkgs/rust.nix create mode 100644 nix/shells.nix create mode 100644 nix/treefmt.nix diff --git a/flake.lock b/flake.lock index b62d84642..2b4169eb0 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,36 @@ { "nodes": { + "advisory-db": { + "flake": false, + "locked": { + "lastModified": 1727353582, + "narHash": "sha256-2csMEEOZhvowVKZNBHk1kMJqk72ZMrPj9LQYCzP6EKs=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "cb905e6e405834bdff1eb1e20c9b10edb5403889", + "type": "github" + }, + "original": { + "owner": "rustsec", + "repo": "advisory-db", + "type": "github" + } + }, + "crane": { + "locked": { + "lastModified": 1727316705, + "narHash": "sha256-/mumx8AQ5xFuCJqxCIOFCHTVlxHkMT21idpbgbm/TIE=", + "owner": "ipetkov", + "repo": "crane", + "rev": "5b03654ce046b5167e7b0bccbd8244cb56c16f0e", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-parts": { "inputs": { "nixpkgs-lib": "nixpkgs-lib" @@ -20,11 +51,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1725693463, - "narHash": "sha256-ZPzhebbWBOr0zRWW10FfqfbJlan3G96/h3uqhiFqmwg=", + "lastModified": 1727540905, + "narHash": "sha256-40J9tW7Y794J7Uw4GwcAKlMxlX2xISBl6IBigo83ih8=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "68e7dce0a6532e876980764167ad158174402c6f", + "rev": "fbca5e745367ae7632731639de5c21f29c8744ed", "type": "github" }, "original": { @@ -46,10 +77,47 @@ "url": "https://github.com/NixOS/nixpkgs/archive/356624c12086a18f2ea2825fed34523d60ccc4e3.tar.gz" } }, + "nixpkgs_2": { + "locked": { + "lastModified": 1726871744, + "narHash": "sha256-V5LpfdHyQkUF7RfOaDPrZDP+oqz88lTJrMT1+stXNwo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "a1d92660c6b3b7c26fb883500a80ea9d33321be2", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { + "advisory-db": "advisory-db", + "crane": "crane", "flake-parts": "flake-parts", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1727431250, + "narHash": "sha256-uGRlRT47ecicF9iLD1G3g43jn2e+b5KaMptb59LHnvM=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "879b29ae9a0378904fbbefe0dadaed43c8905754", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 06d411916..7c44aafe6 100644 --- a/flake.nix +++ b/flake.nix @@ -3,7 +3,17 @@ inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; + flake-parts.url = "github:hercules-ci/flake-parts"; + + crane.url = "github:ipetkov/crane"; + + treefmt-nix.url = "github:numtide/treefmt-nix"; + + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; }; outputs = @@ -15,18 +25,25 @@ }: flake-parts.lib.mkFlake { inherit inputs; } { systems = nixpkgs.lib.systems.flakeExposed; + imports = [ + inputs.treefmt-nix.flakeModule + ./nix/pkgs/flake-module.nix + ./nix/checks/flake-module.nix + ./nix/shells.nix + ./nix/treefmt.nix + ]; perSystem = { config, pkgs, self', + system, ... }: { - packages = rec { - # This package depends on git submodules so use a shell command like 'nix build .?submodules=1'. - cln = pkgs.callPackage nix/pkgs/default.nix { inherit self pkgs; }; - default = cln; + _module.args.pkgs = import inputs.nixpkgs { + inherit system; + overlays = [ (final: prev: { craneLib = (inputs.crane.mkLib pkgs); }) ]; }; apps = { lightningd = { @@ -42,10 +59,6 @@ program = "${self'.packages.cln}/bin/reckless"; }; }; - checks = { - cln = self'.packages.cln; - }; - formatter = pkgs.nixfmt-rfc-style; }; }; } diff --git a/nix/checks/flake-module.nix b/nix/checks/flake-module.nix new file mode 100644 index 000000000..131f5fffc --- /dev/null +++ b/nix/checks/flake-module.nix @@ -0,0 +1,19 @@ +{ inputs, self, ... }: +{ + perSystem = + { pkgs, config, ... }: + let + advisory-db = inputs.advisory-db; + in + { + checks = { + cln = config.packages.cln; + rust = config.packages.rust; + cargo-audit = pkgs.craneLib.cargoAudit { + src = ../../.; + inherit advisory-db; + }; + formatting = config.treefmt.build.check self; + }; + }; +} diff --git a/nix/pkgs/default.nix b/nix/pkgs/default.nix index 51f7f7920..423794eee 100644 --- a/nix/pkgs/default.nix +++ b/nix/pkgs/default.nix @@ -2,6 +2,7 @@ self, lib, pkgs, + config, }: with pkgs; let @@ -28,12 +29,10 @@ stdenv.mkDerivation { autoconf autogen automake - cargo gettext gitMinimal libtool lowdown - protobuf py3 unzip which @@ -83,6 +82,7 @@ stdenv.mkDerivation { # The `clnrest` plugin requires a Python environment to run postInstall = '' rm -r $out/libexec/c-lightning/plugins/clnrest + cp ${config.packages.rust}/bin/cln-grpc $out/libexec/c-lightning/plugins ''; meta = with lib; { diff --git a/nix/pkgs/flake-module.nix b/nix/pkgs/flake-module.nix new file mode 100644 index 000000000..5132f17c7 --- /dev/null +++ b/nix/pkgs/flake-module.nix @@ -0,0 +1,13 @@ +{ self, ... }: +{ + perSystem = + { pkgs, config, ... }: + { + packages = rec { + # This package depends on git submodules so use a shell command like 'nix build .?submodules=1'. + cln = pkgs.callPackage ./default.nix { inherit self pkgs config; }; + rust = pkgs.callPackage ./rust.nix { craneLib = pkgs.craneLib; }; + default = cln; + }; + }; +} diff --git a/nix/pkgs/rust.nix b/nix/pkgs/rust.nix new file mode 100644 index 000000000..653ad680f --- /dev/null +++ b/nix/pkgs/rust.nix @@ -0,0 +1,19 @@ +{ + pkgs, + lib, + craneLib, + ... +}: +let + version = builtins.readFile ../../.version; + src = lib.cleanSourceWith { + src = ../../.; + filter = path: type: (lib.hasSuffix "\.proto" path) || (craneLib.filterCargoSources path type); + }; +in +craneLib.buildPackage { + pname = "rust"; + inherit src version; + strictDeps = true; + nativeBuildInputs = with pkgs; [ protobuf ]; +} diff --git a/nix/shells.nix b/nix/shells.nix new file mode 100644 index 000000000..4f7bb90bc --- /dev/null +++ b/nix/shells.nix @@ -0,0 +1,19 @@ +{ self, ... }: +{ + perSystem = + { + config, + pkgs, + system, + ... + }: + { + devShells = { + rust = pkgs.craneLib.devShell { + checks = { + inherit (self.checks.${system}) rust; + }; + }; + }; + }; +} diff --git a/nix/treefmt.nix b/nix/treefmt.nix new file mode 100644 index 000000000..ef6e99a34 --- /dev/null +++ b/nix/treefmt.nix @@ -0,0 +1,11 @@ +{ ... }: +{ + perSystem = + { pkgs, lib, ... }: + { + treefmt = { + projectRootFile = "flake.nix"; + programs.nixfmt.enable = true; + }; + }; +}