Skip to content

Commit 95c11d1

Browse files
committed
feat: add flake module for users
1 parent 1429863 commit 95c11d1

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
./nix/perSystem-tools/flake-module.nix
6363
./crate2nix/flake-module.nix
6464
./docs/flake-module.nix
65+
./public-flake-module.nix
6566
];
6667

6768
flake = { lib, ... }: {

public-flake-module.nix

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{ self, ... }:
2+
{
3+
flake.flakeModule =
4+
{
5+
lib,
6+
flake-parts-lib,
7+
inputs,
8+
...
9+
}: let
10+
inherit (flake-parts-lib) mkPerSystemOption;
11+
inherit (lib) types;
12+
in {
13+
_file = ./public-flake-module.nix;
14+
15+
options.perSystem = mkPerSystemOption (
16+
perSystem@{
17+
config,
18+
options,
19+
pkgs,
20+
system,
21+
...
22+
}: let
23+
cfg = config.crate2nix;
24+
25+
crate2nix = self.packages.${pkgs.system}.default;
26+
27+
mkCargoNix =
28+
args@{
29+
pkgs ? perSystem.pkgs
30+
, rust ? pkgs.rustc
31+
, cargo ? pkgs.cargo
32+
, target ? null
33+
, ...
34+
}:
35+
import cfg.cargoNix {
36+
pkgs =
37+
if target == null then args.pkgs
38+
else import pkgs.path {
39+
inherit (pkgs) system config overlays;
40+
crossSystem =
41+
if lib.isString target
42+
then { config = target; }
43+
else target;
44+
};
45+
46+
buildRustCrateForPkgs = crate: pkgs.buildRustCrate.override {
47+
inherit cargo;
48+
rustc = rust;
49+
};
50+
defaultCrateOverrides = pkgs.defaultCrateOverrides // cfg.crateOverrides;
51+
};
52+
53+
cargoNix = mkCargoNix {
54+
inherit pkgs;
55+
inherit (cfg.toolchain) rust cargo;
56+
};
57+
58+
getMemberBuild = name: member:
59+
let
60+
crateOverrides =
61+
if cfg.crateOverrides ? ${name}
62+
then cfg.crateOverrides.${name} {}
63+
else {};
64+
overrideAttrNames = builtins.attrNames crateOverrides;
65+
isInOverride = n: lib.elem n overrideAttrNames;
66+
globalBuildAttrs = [ "pkgs" "rust" "cargo" "target" ];
67+
in
68+
if lib.any isInOverride globalBuildAttrs
69+
then
70+
(mkCargoNix crateOverrides).workspaceMembers.${name}.build
71+
else member.build;
72+
in {
73+
options.crate2nix = {
74+
cargoNix = lib.mkOption {
75+
type = types.nullOr types.path;
76+
default = null;
77+
description = ''
78+
Path to Cargo.nix.
79+
'';
80+
};
81+
toolchain = {
82+
rust = lib.mkOption {
83+
type = types.package;
84+
default = pkgs.rustc;
85+
description = ''
86+
Rust compiler to build with.
87+
'';
88+
};
89+
cargo = lib.mkOption {
90+
type = types.package;
91+
default = pkgs.cargo;
92+
description = ''
93+
Cargo command to make available to build processes.
94+
'';
95+
};
96+
};
97+
crateOverrides = lib.mkOption {
98+
type = types.attrsOf (types.anything);
99+
default = {};
100+
description = ''
101+
Crate overrides.
102+
'';
103+
example = ''
104+
{
105+
openssl = attrs: {
106+
nativeBuildInputs = [ pkgs.openssl ];
107+
};
108+
}
109+
'';
110+
};
111+
devshell = lib.mkOption {
112+
type = types.nullOr types.str;
113+
default = null;
114+
description = ''
115+
Name of devshell to add update-cargo-nix command to.
116+
'';
117+
};
118+
};
119+
config = lib.mkMerge [
120+
(lib.mkIf (options ? devshells && cfg.devshell != null) (
121+
(lib.optionalAttrs (options ? devshells) {
122+
devshells.${cfg.devshell} = {
123+
commands = [
124+
{
125+
category = "development";
126+
name = "update-cargo-nix";
127+
help = "Update Cargo.nix";
128+
command = "${lib.getExe crate2nix} generate";
129+
}
130+
];
131+
};
132+
})
133+
))
134+
# Allow for just adding devshell to get access to update-cargo-nix
135+
(lib.mkIf (cfg.cargoNix != null) {
136+
packages = lib.mapAttrs getMemberBuild cargoNix.workspaceMembers;
137+
})
138+
];
139+
}
140+
);
141+
};
142+
}

0 commit comments

Comments
 (0)