Shell scripts: Difference between revisions

Pigs (talk | contribs)
m Add category development and shell
DHCP (talk | contribs)
m capitalize sentences
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
the package <code>writeShellScript</code> can be used to add shell scripts to nix expressions
The package <code>writeShellScript</code> can be used to add shell scripts to nix expressions


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
  someBuildHelper = { name, sha256 }:
someBuildHelper = { name, sha256 }:
    stdenv.mkDerivation {
  stdenv.mkDerivation {
      inherit name;
    inherit name;
      outputHashMode = "recursive";
    outputHashMode = "recursive";
      outputHashAlgo = "sha256";
    outputHashAlgo = "sha256";
      outputHash = sha256;
    outputHash = sha256;
      builder = writeShellScript "builder.sh" ''
    builder = writeShellScript "builder.sh" ''
        echo "hi, my name is ''${0}" # escape bash variable
      echo "hi, my name is ''${0}" # escape bash variable
        echo "hi, my hash is ${sha256}" # use nix variable
      echo "hi, my hash is ${sha256}" # use nix variable
        echo "hello world" >output.txt
      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:


<syntaxHighlight lang="nix">
{{file|default.nix|nix|3=
# default.nix
{
{
   outputTxtDrv = stdenv.mkDerivation rec {
   outputTxtDrv = stdenv.mkDerivation rec {
Line 36: Line 35:
   };
   };
}
}
</syntaxHighlight>
}}


<syntaxHighlight lang="bash">
{{file|builder.sh|sh|3=
# builder.sh
echo "$someString $(echo "$someJson" | jq -r '.dst') $someNumber" >$out
echo "$someString $(echo "$someJson" | jq -r '.dst') $someNumber" >$out
</syntaxHighlight>
}}


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:


<syntaxHighlight lang="nix">
{{file|default.nix|nix|3=
# default.nix
{
{
   outputTxtDrv = runCommand "output.txt" {
   outputTxtDrv = runCommand "output.txt" {
Line 63: Line 60:
   } (builtins.readFile ./builder.sh);
   } (builtins.readFile ./builder.sh);
}
}
</syntaxHighlight>
}}


== Packaging ==
== Packaging ==


example:
Example:


<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
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.
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:
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                                         0 s /nix/store/vr96j3cxj75xsczl8pzrgsv1k57hcxyp-coreutils-8.31/bin/stat
coreutils.out       0 s /nix/store/vr96j3cxj75xsczl8pzrgsv1k57hcxyp-coreutils-8.31/bin/stat
</syntaxHighlight>
</syntaxHighlight>


Line 119: Line 116:
To trace commands and line numbers, we can use
To trace commands and line numbers, we can use


<syntaxHighlight lang="nix">
{{file|test-trace.nix|nix|3=
# test-trace.nix
{ runCommand, coreutils }:
{ runCommand, coreutils }:
  runCommand "output.txt" {
runCommand "output.txt" { nativeBuildInputs = [ coreutils ]; } ''
    nativeBuildInputs = [ coreutils ];
  # line 5 in nix file = line 1 in bash script -> offset 4
  } ''
  PS4='+ Line $(expr $LINENO + 4): '
    # line 5 in nix file = line 1 in bash script -> offset 4
  set -o xtrace # print commands
    PS4='+ Line $(expr $LINENO + 4): '
    set -o xtrace # print commands


    echo hello >$out # line 9 in nix file
  echo hello >$out # line 9 in nix file


    set +o xtrace # hide commands
  set +o xtrace # hide commands
  ''
''
</syntaxHighlight>
 
}}


<syntaxHighlight lang="console">
<syntaxHighlight lang="console">
Line 147: Line 142:
== 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>).
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>
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://gist.github.com/travisbhartwell/f972aab227306edfcfea nix-shell and Shebang Lines]