build: Add nix derivation for building cargo workspace

This commit is contained in:
Joseph Goulden 2024-09-29 15:01:08 +01:00 committed by Vincenzo Palazzo
parent 2056478e56
commit 864ece025e
8 changed files with 176 additions and 14 deletions

View File

@ -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"
}
}
},

View File

@ -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;
};
};
}

View File

@ -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;
};
};
}

View File

@ -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; {

13
nix/pkgs/flake-module.nix Normal file
View File

@ -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;
};
};
}

19
nix/pkgs/rust.nix Normal file
View File

@ -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 ];
}

19
nix/shells.nix Normal file
View File

@ -0,0 +1,19 @@
{ self, ... }:
{
perSystem =
{
config,
pkgs,
system,
...
}:
{
devShells = {
rust = pkgs.craneLib.devShell {
checks = {
inherit (self.checks.${system}) rust;
};
};
};
};
}

11
nix/treefmt.nix Normal file
View File

@ -0,0 +1,11 @@
{ ... }:
{
perSystem =
{ pkgs, lib, ... }:
{
treefmt = {
projectRootFile = "flake.nix";
programs.nixfmt.enable = true;
};
};
}