-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflake-module.nix
78 lines (77 loc) · 2.44 KB
/
flake-module.nix
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
{ self, lib, flake-parts-lib, ... }:
let
inherit (flake-parts-lib)
mkPerSystemOption;
inherit (lib)
mkOption
types;
in
{
options = {
perSystem = mkPerSystemOption
({ config, self', inputs', pkgs, system, ... }:
let
mainSubmodule = types.submodule {
options = {
projectRootFile = mkOption {
type = types.str;
description = "The name of the unique file that exists only at project root";
default = "flake.nix";
};
devShell = mkOption {
type = types.package;
readOnly = true;
description = ''
Devshell providing a shellHook setting $FLAKE_ROOT
'';
default = pkgs.mkShell {
name = "flake-root-devshell";
shellHook = ''
FLAKE_ROOT="''$(${lib.getExe config.flake-root.package})"
export FLAKE_ROOT
'';
};
};
package = mkOption {
type = types.package;
readOnly = true;
description = ''
The Nix package providing the command to find the project root.
'';
default = pkgs.writeShellApplication {
name = "flake-root";
text = ''
set -euo pipefail
find_up() {
ancestors=()
while true; do
if [[ -f $1 ]]; then
echo "$PWD"
exit 0
fi
ancestors+=("$PWD")
if [[ $PWD == / ]] || [[ $PWD == // ]]; then
echo "ERROR: Unable to locate the ${config.flake-root.projectRootFile} in any of: ''${ancestors[*]@Q}" >&2
exit 1
fi
cd ..
done
}
find_up "${config.flake-root.projectRootFile}"
'';
};
};
};
};
in
{
options.flake-root = lib.mkOption {
type = mainSubmodule;
description = ''
flake-root module options
'';
default = { };
};
});
};
}