Skip to content

Commit 864ece0

Browse files
JosephGouldenvincenzopalazzo
authored andcommitted
build: Add nix derivation for building cargo workspace
1 parent 2056478 commit 864ece0

File tree

8 files changed

+176
-14
lines changed

8 files changed

+176
-14
lines changed

flake.lock

Lines changed: 72 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33

44
inputs = {
55
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
6+
67
flake-parts.url = "github:hercules-ci/flake-parts";
8+
9+
crane.url = "github:ipetkov/crane";
10+
11+
treefmt-nix.url = "github:numtide/treefmt-nix";
12+
13+
advisory-db = {
14+
url = "github:rustsec/advisory-db";
15+
flake = false;
16+
};
717
};
818

919
outputs =
@@ -15,18 +25,25 @@
1525
}:
1626
flake-parts.lib.mkFlake { inherit inputs; } {
1727
systems = nixpkgs.lib.systems.flakeExposed;
28+
imports = [
29+
inputs.treefmt-nix.flakeModule
30+
./nix/pkgs/flake-module.nix
31+
./nix/checks/flake-module.nix
32+
./nix/shells.nix
33+
./nix/treefmt.nix
34+
];
1835
perSystem =
1936
{
2037
config,
2138
pkgs,
2239
self',
40+
system,
2341
...
2442
}:
2543
{
26-
packages = rec {
27-
# This package depends on git submodules so use a shell command like 'nix build .?submodules=1'.
28-
cln = pkgs.callPackage nix/pkgs/default.nix { inherit self pkgs; };
29-
default = cln;
44+
_module.args.pkgs = import inputs.nixpkgs {
45+
inherit system;
46+
overlays = [ (final: prev: { craneLib = (inputs.crane.mkLib pkgs); }) ];
3047
};
3148
apps = {
3249
lightningd = {
@@ -42,10 +59,6 @@
4259
program = "${self'.packages.cln}/bin/reckless";
4360
};
4461
};
45-
checks = {
46-
cln = self'.packages.cln;
47-
};
48-
formatter = pkgs.nixfmt-rfc-style;
4962
};
5063
};
5164
}

nix/checks/flake-module.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ inputs, self, ... }:
2+
{
3+
perSystem =
4+
{ pkgs, config, ... }:
5+
let
6+
advisory-db = inputs.advisory-db;
7+
in
8+
{
9+
checks = {
10+
cln = config.packages.cln;
11+
rust = config.packages.rust;
12+
cargo-audit = pkgs.craneLib.cargoAudit {
13+
src = ../../.;
14+
inherit advisory-db;
15+
};
16+
formatting = config.treefmt.build.check self;
17+
};
18+
};
19+
}

nix/pkgs/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
self,
33
lib,
44
pkgs,
5+
config,
56
}:
67
with pkgs;
78
let
@@ -28,12 +29,10 @@ stdenv.mkDerivation {
2829
autoconf
2930
autogen
3031
automake
31-
cargo
3232
gettext
3333
gitMinimal
3434
libtool
3535
lowdown
36-
protobuf
3736
py3
3837
unzip
3938
which
@@ -83,6 +82,7 @@ stdenv.mkDerivation {
8382
# The `clnrest` plugin requires a Python environment to run
8483
postInstall = ''
8584
rm -r $out/libexec/c-lightning/plugins/clnrest
85+
cp ${config.packages.rust}/bin/cln-grpc $out/libexec/c-lightning/plugins
8686
'';
8787

8888
meta = with lib; {

nix/pkgs/flake-module.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ self, ... }:
2+
{
3+
perSystem =
4+
{ pkgs, config, ... }:
5+
{
6+
packages = rec {
7+
# This package depends on git submodules so use a shell command like 'nix build .?submodules=1'.
8+
cln = pkgs.callPackage ./default.nix { inherit self pkgs config; };
9+
rust = pkgs.callPackage ./rust.nix { craneLib = pkgs.craneLib; };
10+
default = cln;
11+
};
12+
};
13+
}

nix/pkgs/rust.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
pkgs,
3+
lib,
4+
craneLib,
5+
...
6+
}:
7+
let
8+
version = builtins.readFile ../../.version;
9+
src = lib.cleanSourceWith {
10+
src = ../../.;
11+
filter = path: type: (lib.hasSuffix "\.proto" path) || (craneLib.filterCargoSources path type);
12+
};
13+
in
14+
craneLib.buildPackage {
15+
pname = "rust";
16+
inherit src version;
17+
strictDeps = true;
18+
nativeBuildInputs = with pkgs; [ protobuf ];
19+
}

nix/shells.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{ self, ... }:
2+
{
3+
perSystem =
4+
{
5+
config,
6+
pkgs,
7+
system,
8+
...
9+
}:
10+
{
11+
devShells = {
12+
rust = pkgs.craneLib.devShell {
13+
checks = {
14+
inherit (self.checks.${system}) rust;
15+
};
16+
};
17+
};
18+
};
19+
}

nix/treefmt.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{ ... }:
2+
{
3+
perSystem =
4+
{ pkgs, lib, ... }:
5+
{
6+
treefmt = {
7+
projectRootFile = "flake.nix";
8+
programs.nixfmt.enable = true;
9+
};
10+
};
11+
}

0 commit comments

Comments
 (0)