-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflakeModule.nix
More file actions
81 lines (73 loc) · 2.66 KB
/
Copy pathflakeModule.nix
File metadata and controls
81 lines (73 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{
inputs,
lib,
config,
...
}: let
inherit (lib) filterAttrs mapAttrs optionalAttrs mkDefault mkOption pipe elem;
inherit (lib.types) listOf str bool;
inherit (builtins) attrNames;
cfg = config.flake-follows;
rootInputs = filterAttrs (n: _: n != "self") inputs;
exclude = map (lib.replaceStrings ["/"] ["."]) cfg.exclude;
autoFollowsFor = name:
pipe (attrNames (inputs.${name}.inputs or {})) [
(map (subName:
lib.nameValuePair subName (
if elem "${name}.${subName}" exclude
then null
else if inputs ? ${subName}
then subName
else null
)))
lib.listToAttrs
(filterAttrs (_: v: v != null))
];
in {
options.flake-follows = {
exclude = mkOption {
type = listOf str;
default = [];
example = ["hyprland/nixpkgs"];
description = "Sub-inputs to never auto-follow, in 'input/subInput' or 'input.subInput' form.";
};
autoLock = mkOption {
type = bool;
default = true;
description = "Run `nix flake lock` after writing flake.nix to lock any new inputs, then regenerate once if the lock changed.";
};
};
config = {
flake-file.inputs = mapAttrs (
name: _: let
auto = autoFollowsFor name;
in
optionalAttrs (auto != {}) {
inputs = mapAttrs (_: target: {follows = mkDefault target;}) auto;
}
)
rootInputs;
# Removed inputs linger in `inputs` from the old flake.nix; drop their orphaned follows.
flake-file.preProcess = mkDefault (
filterAttrs (_: v: v ? url || v ? follows)
);
flake-file.write-hooks = lib.mkIf cfg.autoLock [
{
index = 50;
program = pkgs:
pkgs.writeShellApplication {
name = "flake-follows-auto-lock";
text = ''
# Guard against re-entry from the second write-flake below.
[ -n "''${FLAKE_FOLLOWS_REGEN:-}" ] && exit 0
# Fast path: if the lock already covers all inputs, nothing to do.
nix flake lock --no-update-lock-file 2>/dev/null && exit 0
export FLAKE_FOLLOWS_REGEN=1
nix flake lock
nix run .#write-flake
'';
};
}
];
};
}