query
On this page

writeShellApplication

pkgs.writeShellApplication

Docs pulled from | This Revision | about 1 hour ago


Nixpkgs manual

or https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeShellApplication

Noogle detected

Implementation

The following is the current implementation of this function.

writeShellApplication =
    {
      name,
      text,
      runtimeInputs ? [ ],
      runtimeEnv ? null,
      meta ? { },
      passthru ? { },
      checkPhase ? null,
      excludeShellChecks ? [ ],
      extraShellCheckFlags ? [ ],
      bashOptions ? [
        "errexit"
        "nounset"
        "pipefail"
      ],
      derivationArgs ? { },
      inheritPath ? true,
    }@args:
    writeTextFile {
      pos = builtins.unsafeGetAttrPos "name" args;
      inherit
        name
        meta
        passthru
        derivationArgs
        ;
      executable = true;
      destination = "/bin/${name}";
      allowSubstitutes = true;
      preferLocalBuild = false;
      text = ''
        #!${runtimeShell}
        ${lib.concatMapStringsSep "\n" (option: "set -o ${option}") bashOptions}
      ''
      + lib.optionalString (runtimeEnv != null) (
        lib.concatMapAttrsStringSep "" (name: value: ''
          ${lib.toShellVar name value}
          export ${name}
        '') runtimeEnv
      )
      + ''

        export PATH="${
          lib.concatStringsSep ":" (
            (lib.optionals (runtimeInputs != [ ]) [ (lib.makeBinPath runtimeInputs) ])
            ++ (lib.optionals inheritPath [ "$PATH" ])
          )
        }"
      ''

      + ''

        ${text}
      '';

      checkPhase =
        let
          excludeFlags = lib.optionals (excludeShellChecks != [ ]) [
            "--exclude"
            (lib.concatStringsSep "," excludeShellChecks)
          ];
          # GHC (=> shellcheck) isn't supported on some platforms (such as risc-v)
          # but we still want to use writeShellApplication on those platforms
          shellcheckCommand = lib.optionalString shellcheck-minimal.compiler.bootstrapAvailable ''
            # use shellcheck which does not include docs
            # pandoc takes long to build and documentation isn't needed for just running the cli
            ${lib.getExe shellcheck-minimal} ${
              lib.escapeShellArgs (excludeFlags ++ extraShellCheckFlags)
            } "$target"
          '';
        in
        if checkPhase == null then
          ''
            runHook preCheck
            ${stdenv.shellDryRun} "$target"
            ${shellcheckCommand}
            runHook postCheck
          ''
        else
          checkPhase;
    };