Shell Scripts: Difference between revisions

From NixOS Wiki
imported>Milahu
(+ Packaging example)
imported>Milahu
(+ Command not found)
Line 51: Line 51:
</syntaxHighlight>
</syntaxHighlight>


when the dependency <code>subversion</code> is missing, the script throws the error <code>svn: command not found</code>
<code>wrapProgram</code> will move the original script to <code>.github-downloader.sh-wrapped</code>


<code>wrapProgram</code> will move the original script to <code>.github-downloader.sh-wrapped</code>
=== 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">
$ nix-locate bin/stat | grep 'bin/stat$'
coreutils.out                                        0 s /nix/store/vr96j3cxj75xsczl8pzrgsv1k57hcxyp-coreutils-8.31/bin/stat
</syntaxHighlight>


== Posix Shell ==
== Posix Shell ==

Revision as of 09:59, 1 October 2021

the package writeShellScript can be used to add shell scripts to nix expressions

  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
      '';
    };

Packaging

example:

# nix-build -E 'with import <nixpkgs> { }; callPackage ./default.nix { }'

{ stdenv
, lib
, fetchFromGitHub
, bash
, subversion
, makeWrapper
}:
  stdenv.mkDerivation {
    pname = "github-downloader";
    version = "08049f6";
    src = fetchFromGitHub {
      # https://github.com/Decad/github-downloader
      owner = "Decad";
      repo = "github-downloader";
      rev = "08049f6183e559a9a97b1d144c070a36118cca97";
      sha256 = "073jkky5svrb7hmbx3ycgzpb37hdap7nd9i0id5b5yxlcnf7930r";
    };
    buildInputs = [ bash subversion ];
    nativeBuildInputs = [ makeWrapper ];
    installPhase = ''
      mkdir -p $out/bin
      cp github-downloader.sh $out/bin/github-downloader.sh
      wrapProgram $out/bin/github-downloader.sh \
        --prefix PATH : ${lib.makeBinPath [ bash subversion ]}
    '';
  };

wrapProgram will move the original script to .github-downloader.sh-wrapped

Command not found

for example, the script throws the error svn: command not found, because the dependency subversion is missing.

when a command is missing, you can use nix-locate to find the package name. for example, the stat command:

$ nix-locate bin/stat | grep 'bin/stat$'
coreutils.out                                         0 s /nix/store/vr96j3cxj75xsczl8pzrgsv1k57hcxyp-coreutils-8.31/bin/stat

Posix Shell

some environments (like OpenWRT, via busybox) offer only a "limited" shell (sh instead of bash).

on nixos, posix shells are provided by the packages dash and posh

See also