query
On this page

writeTextFile

pkgs.writeTextFile

Docs pulled from | This Revision | 10 minutes ago


See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeTextFile


Noogle detected

Implementation

The following is the current implementation of this function.

writeTextFile =
    {
      name,
      text,
      executable ? false,
      destination ? "",
      checkPhase ? "",
      meta ? { },
      passthru ? { },
      allowSubstitutes ? false,
      preferLocalBuild ? true,
      derivationArgs ? { },
    }:
    assert lib.assertMsg (destination != "" -> (lib.hasPrefix "/" destination && destination != "/")) ''
      destination must be an absolute path, relative to the derivation's out path,
      got '${destination}' instead.

      Ensure that the path starts with a / and specifies at least the filename.
    '';

    let
      matches = builtins.match "/bin/([^/]+)" destination;
    in
    runCommand name
      (
        {
          inherit
            text
            executable
            checkPhase
            allowSubstitutes
            preferLocalBuild
            ;
          passAsFile = [ "text" ] ++ derivationArgs.passAsFile or [ ];
          meta =
            lib.optionalAttrs (executable && matches != null) {
              mainProgram = lib.head matches;
            }
            // meta
            // derivationArgs.meta or { };
          passthru = passthru // derivationArgs.passthru or { };
        }
        // removeAttrs derivationArgs [
          "passAsFile"
          "meta"
          "passthru"
        ]
      )
      ''
        target=$out${lib.escapeShellArg destination}
        mkdir -p "$(dirname "$target")"

        if [ -e "$textPath" ]; then
          mv "$textPath" "$target"
        else
          echo -n "$text" > "$target"
        fi

        if [ -n "$executable" ]; then
          chmod +x "$target"
        fi

        eval "$checkPhase"
      '';