Skip to content

Commit

Permalink
build: Add nix derivation for building cargo workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephGoulden authored and vincenzopalazzo committed Oct 21, 2024
1 parent 2056478 commit 864ece0
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 14 deletions.
76 changes: 72 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 = {
Expand All @@ -42,10 +59,6 @@
program = "${self'.packages.cln}/bin/reckless";
};
};
checks = {
cln = self'.packages.cln;
};
formatter = pkgs.nixfmt-rfc-style;
};
};
}
19 changes: 19 additions & 0 deletions nix/checks/flake-module.nix
Original file line number Diff line number Diff line change
@@ -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;
};
};
}
4 changes: 2 additions & 2 deletions nix/pkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
self,
lib,
pkgs,
config,
}:
with pkgs;
let
Expand All @@ -28,12 +29,10 @@ stdenv.mkDerivation {
autoconf
autogen
automake
cargo
gettext
gitMinimal
libtool
lowdown
protobuf
py3
unzip
which
Expand Down Expand Up @@ -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; {
Expand Down
13 changes: 13 additions & 0 deletions nix/pkgs/flake-module.nix
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions nix/pkgs/rust.nix
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions nix/shells.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{ self, ... }:
{
perSystem =
{
config,
pkgs,
system,
...
}:
{
devShells = {
rust = pkgs.craneLib.devShell {
checks = {
inherit (self.checks.${system}) rust;
};
};
};
};
}
11 changes: 11 additions & 0 deletions nix/treefmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{ ... }:
{
perSystem =
{ pkgs, lib, ... }:
{
treefmt = {
projectRootFile = "flake.nix";
programs.nixfmt.enable = true;
};
};
}

0 comments on commit 864ece0

Please sign in to comment.