forked from cachix/devenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.nix
71 lines (66 loc) · 1.85 KB
/
scripts.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
{ lib
, config
, pkgs
, ...
}:
let
types = lib.types;
scriptType = types.submodule (
{ config, name, ... }:
{
options = {
exec = lib.mkOption {
type = types.str;
description = "Shell code to execute when the script is run.";
};
package = lib.mkOption {
type = types.package;
description = "The package to use to run the script.";
default = pkgs.bash;
defaultText = lib.literalExpression "pkgs.bash";
};
binary = lib.mkOption {
type = types.nullOr types.str;
description = "Override the binary name from the default `package.meta.mainProgram`";
default = null;
};
description = lib.mkOption {
type = types.str;
description = "Description of the script.";
default = "";
};
scriptPackage = lib.mkOption {
internal = true;
type = types.package;
};
};
config.scriptPackage = let
binary =
if config.binary != null
then "${pkgs.lib.getBin config.package}/bin/${config.binary}"
else pkgs.lib.getExe config.package;
in
lib.hiPrioSet (
pkgs.writeScriptBin name ''
#!${binary}
${config.exec}
''
);
}
);
renderInfoSection = name: script:
"${name}${lib.optionalString (script.description != "") ": ${script.description}"} ${script.scriptPackage}";
in
{
options = {
scripts = lib.mkOption {
type = types.attrsOf scriptType;
default = { };
description = "A set of scripts available when the environment is active.";
};
};
config = {
packages = lib.mapAttrsToList (_: script: script.scriptPackage) config.scripts;
infoSections."scripts" = lib.mapAttrsToList renderInfoSection config.scripts;
};
}