Skip to content

Commit feabb0d

Browse files
authored
Merge pull request #2133 from cachix/fix-2127
scripts: fix the `packages` option
2 parents 2946cff + c50299c commit feabb0d

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

examples/scripts/devenv.nix

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
}:
66

77
{
8+
# Top-level packages to the shell
89
packages = [
9-
pkgs.curl
1010
pkgs.jq
1111
];
1212

13-
scripts.silly-example.exec = ''curl "https://httpbin.org/get?$1" | jq .args'';
14-
scripts.silly-example.description = "curls httpbin with provided arg";
13+
# Scripts have access to the top-level `packages`
14+
scripts.silly-example.exec = ''echo "{\"name\":\"$1\",\"greeting\":\"Hello $1!\",\"timestamp\":\"$(date -Iseconds)\"}" | jq '';
15+
scripts.silly-example.description = "creates JSON with provided arg and shows it with jq";
1516

16-
scripts.serious-example.exec = ''${pkgs.cowsay}/bin/cowsay "$*"'';
17+
# Scripts can declare their own private `packages`
18+
scripts.serious-example.exec = ''cowsay "$*"'';
19+
scripts.serious-example.packages = [ pkgs.cowsay ];
1720
scripts.serious-example.description = ''echoes args in a very serious manner'';
1821

19-
# Example with custom package
22+
# Write scripts using your favourite language.
2023
scripts.python-hello.exec = ''print("Hello, world!")'';
21-
scripts.python-hello.package = pkgs.python311;
24+
scripts.python-hello.package = pkgs.python3Minimal;
2225

23-
# Example when package and binary are different
26+
# Handle custom scripts where the binary name doesn't match the package name
2427
scripts.nushell-greet.exec = ''
2528
def greet [name] {
2629
["hello" $name]
@@ -31,6 +34,7 @@
3134
scripts.nushell-greet.package = pkgs.nushell;
3235
scripts.nushell-greet.binary = "nu";
3336

37+
# Render a help section when you enter the shell, similar to `devenv info`
3438
enterShell = ''
3539
echo
3640
echo 🦾 Helper scripts you can run to make your development richer:
@@ -40,4 +44,19 @@
4044
EOF
4145
echo
4246
'';
47+
48+
# Test that the scripts work as expected with `devenv test`
49+
enterTest = ''
50+
echo "Testing silly-example"
51+
silly-example world | grep Hello
52+
53+
echo "Testing serious-example"
54+
serious-example hello world | grep hello
55+
56+
echo "Testing python-hello"
57+
python-hello | grep Hello
58+
59+
echo "Testing nushell-greet"
60+
nushell-greet | grep hello
61+
'';
4362
}

src/modules/scripts.nix

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,17 @@ let
4848
if config.binary != null
4949
then "${pkgs.lib.getBin config.package}/bin/${config.binary}"
5050
else pkgs.lib.getExe config.package;
51+
52+
execScript = pkgs.writeScript "${name}-script" ''
53+
#!${binary}
54+
${config.exec}
55+
'';
5156
in
5257
lib.hiPrioSet (
53-
pkgs.runCommand name
54-
{
55-
buildInputs = config.packages;
56-
text = ''
57-
#!${binary}
58-
${config.exec}
59-
'';
60-
passAsFile = [ "text" ];
61-
meta.mainProgram = name;
62-
} ''
63-
target="$out/bin/${name}"
64-
mkdir -p "$(dirname "$target")"
65-
mv "$textPath" "$target"
66-
chmod +x "$target"
58+
pkgs.writeScriptBin name ''
59+
#!${lib.getExe pkgs.bashInteractive}
60+
export PATH=${pkgs.lib.makeBinPath config.packages}:$PATH
61+
exec ${execScript} "$@"
6762
''
6863
);
6964
}

0 commit comments

Comments
 (0)