query
On this page

sanitizeDerivationName

lib.strings.sanitizeDerivationName

Docs pulled from | This Revision | 21 minutes ago


Creates a valid derivation name from a potentially invalid one.

Inputs

string
1. Function argument

Type

sanitizeDerivationName :: String -> String

Examples

lib.strings.sanitizeDerivationName usage example

sanitizeDerivationName "../hello.bar # foo"
=> "-hello.bar-foo"
sanitizeDerivationName ""
=> "unknown"
sanitizeDerivationName pkgs.hello
=> "-nix-store-2g75chlbpxlrqn15zlby2dfh8hr9qwbk-hello-2.10"

Noogle detected

Implementation

The following is the current implementation of this function.

sanitizeDerivationName =
  let okRegex = match "[[:alnum:]+_?=-][[:alnum:]+._?=-]*";
  in
  string:
  # First detect the common case of already valid strings, to speed those up
  if stringLength string <= 207 && okRegex string != null
  then unsafeDiscardStringContext string
  else lib.pipe string [
    # Get rid of string context. This is safe under the assumption that the
    # resulting string is only used as a derivation name
    unsafeDiscardStringContext
    # Strip all leading "."
    (x: elemAt (match "\\.*(.*)" x) 0)
    # Split out all invalid characters
    # https://github.com/NixOS/nix/blob/2.3.2/src/libstore/store-api.cc#L85-L112
    # https://github.com/NixOS/nix/blob/2242be83c61788b9c0736a92bb0b5c7bbfc40803/nix-rust/src/store/path.rs#L100-L125
    (split "[^[:alnum:]+._?=-]+")
    # Replace invalid character ranges with a "-"
    (concatMapStrings (s: if lib.isList s then "-" else s))
    # Limit to 211 characters (minus 4 chars for ".drv")
    (x: substring (lib.max (stringLength x - 207) 0) (-1) x)
    # If the result is empty, replace it with "unknown"
    (x: if stringLength x == 0 then "unknown" else x)
  ];