Shell scripts: Difference between revisions
imported>Milahu + runCommand + builder.sh, fix links |
m capitalize sentences |
||
| (9 intermediate revisions by 5 users not shown) | |||
| Line 1: | Line 1: | ||
The package <code>writeShellScript</code> can be used to add shell scripts to nix expressions | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
someBuildHelper = { name, sha256 }: | |||
stdenv.mkDerivation { | |||
inherit name; | |||
outputHashMode = "recursive"; | |||
outputHashAlgo = "sha256"; | |||
outputHash = sha256; | |||
builder = writeShellScript "builder.sh" '' | |||
echo "hi, my name is ''${0}" # escape bash variable | |||
echo "hi, my hash is ${sha256}" # use nix variable | |||
echo "hello world" >output.txt | |||
''; | |||
}; | |||
</syntaxHighlight> | </syntaxHighlight> | ||
| Line 20: | Line 20: | ||
Longer bash scripts are usually stored as external script files, and called from Nix: | Longer bash scripts are usually stored as external script files, and called from Nix: | ||
{{file|default.nix|nix|3= | |||
{ | { | ||
outputTxtDrv = stdenv.mkDerivation rec { | |||
name = "output.txt"; | name = "output.txt"; | ||
# disable unpackPhase etc | # disable unpackPhase etc | ||
| Line 36: | Line 35: | ||
}; | }; | ||
} | } | ||
}} | |||
{{file|builder.sh|sh|3= | |||
echo "$someString $(echo "$someJson" | jq -r '.dst') $someNumber" >$out | echo "$someString $(echo "$someJson" | jq -r '.dst') $someNumber" >$out | ||
}} | |||
See also | See also | ||
| Line 52: | Line 50: | ||
Instead of <code>stdenv.mkDerivation</code>, we can also use <code>runCommand</code> to call an external bash script: | Instead of <code>stdenv.mkDerivation</code>, we can also use <code>runCommand</code> to call an external bash script: | ||
{{file|default.nix|nix|3= | |||
{ | { | ||
outputTxtDrv = runCommand "output.txt" { | |||
nativeBuildInputs = [ coreutils jq ]; | nativeBuildInputs = [ coreutils jq ]; | ||
# only strings can be passed to builder | # only strings can be passed to builder | ||
| Line 63: | Line 60: | ||
} (builtins.readFile ./builder.sh); | } (builtins.readFile ./builder.sh); | ||
} | } | ||
}} | |||
== Packaging == | == Packaging == | ||
Example: | |||
<syntaxHighlight lang="nix"> | <syntaxHighlight lang="nix"> | ||
| Line 97: | Line 94: | ||
--prefix PATH : ${lib.makeBinPath [ bash subversion ]} | --prefix PATH : ${lib.makeBinPath [ bash subversion ]} | ||
''; | ''; | ||
} | } | ||
</syntaxHighlight> | </syntaxHighlight> | ||
| Line 104: | Line 101: | ||
=== Command not found === | === Command not found === | ||
For example, the script throws the error <code>svn: command not found</code>, because the dependency <code>subversion</code> is missing. | |||
When a command is missing, you can use <code>nix-locate</code> to find the package name. for example, the <code>stat</code> command: | |||
<syntaxHighlight lang="console"> | <syntaxHighlight lang="console"> | ||
$ nix-locate bin/stat | grep 'bin/stat$' | $ nix-locate bin/stat | grep 'bin/stat$' | ||
coreutils.out | coreutils.out 0 s /nix/store/vr96j3cxj75xsczl8pzrgsv1k57hcxyp-coreutils-8.31/bin/stat | ||
</syntaxHighlight> | |||
== Debugging embedded scripts == | |||
When a bash script fails, it prints only an error message, but no code location. | |||
To trace commands and line numbers, we can use | |||
{{file|test-trace.nix|nix|3= | |||
{ runCommand, coreutils }: | |||
runCommand "output.txt" { nativeBuildInputs = [ coreutils ]; } '' | |||
# line 5 in nix file = line 1 in bash script -> offset 4 | |||
PS4='+ Line $(expr $LINENO + 4): ' | |||
set -o xtrace # print commands | |||
echo hello >$out # line 9 in nix file | |||
set +o xtrace # hide commands | |||
'' | |||
}} | |||
<syntaxHighlight lang="console"> | |||
$ nix-build -E 'with import <nixpkgs> { }; callPackage ./test-trace.nix { }' | |||
this derivation will be built: | |||
/nix/store/2v5biwny8plpyk2bv6cfr41ppp0a1i4k-output.txt.drv | |||
building '/nix/store/2v5biwny8plpyk2bv6cfr41ppp0a1i4k-output.txt.drv'... | |||
++ Line 9: echo hello | |||
++ Line 11: set +o xtrace | |||
/nix/store/ppidmnpd5m762x9kqj8jd3g7df7dknrz-output.txt | |||
</syntaxHighlight> | </syntaxHighlight> | ||
== Posix Shell == | == Posix Shell == | ||
Some environments (like OpenWRT, via <code>busybox</code>) offer only a "limited" shell (<code>sh</code> instead of <code>bash</code>). | |||
On NixOS, posix shells are provided by the packages <code>dash</code> and <code>posh</code>. | |||
== See also == | == See also == | ||
* [[Nix-shell shebang]] | |||
* [https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-functions Shell functions section in the Nixpkgs manual] | * [https://nixos.org/manual/nixpkgs/stable/#ssec-stdenv-functions Shell functions section in the Nixpkgs manual] | ||
* [https://gist.github.com/travisbhartwell/f972aab227306edfcfea nix-shell and Shebang Lines] | |||
* [https://ertt.ca/nix/shell-scripts/ Shell Scripts with Nix] | |||
[[Category:Development]] | |||
[[Category:Shell]] | |||