From 546fc6724237ba93fab6417d3564f7e90f3f77da Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Wed, 27 Mar 2024 22:02:32 -0700 Subject: [PATCH] Avoid top-level `with ...;` in nixos/lib/utils.nix --- nixos/lib/utils.nix | 61 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/nixos/lib/utils.nix b/nixos/lib/utils.nix index 22a2c79843c6..4992113bdbd2 100644 --- a/nixos/lib/utils.nix +++ b/nixos/lib/utils.nix @@ -1,9 +1,44 @@ -{ lib, config, pkgs }: with lib; +{ lib, config, pkgs }: + +let + inherit (lib) + any + attrNames + concatMapStringsSep + concatStringsSep + elem + escapeShellArg + filter + flatten + getName + hasPrefix + hasSuffix + imap0 + imap1 + isAttrs + isDerivation + isFloat + isInt + isList + isPath + isString + listToAttrs + nameValuePair + optionalString + removePrefix + removeSuffix + replaceStrings + stringToCharacters + types + ; + + inherit (lib.strings) toJSON normalizePath escapeC; +in rec { # Copy configuration files to avoid having the entire sources in the system closure - copyFile = filePath: pkgs.runCommand (builtins.unsafeDiscardStringContext (builtins.baseNameOf filePath)) {} '' + copyFile = filePath: pkgs.runCommand (builtins.unsafeDiscardStringContext (baseNameOf filePath)) {} '' cp ${filePath} $out ''; @@ -46,11 +81,11 @@ rec { escapeSystemdPath = s: let replacePrefix = p: r: s: (if (hasPrefix p s) then r + (removePrefix p s) else s); trim = s: removeSuffix "/" (removePrefix "/" s); - normalizedPath = strings.normalizePath s; + normalizedPath = normalizePath s; in replaceStrings ["/"] ["-"] - (replacePrefix "." (strings.escapeC ["."] ".") - (strings.escapeC (stringToCharacters " !\"#$%&'()*+,;<=>=@[\\]^`{|}~-") + (replacePrefix "." (escapeC ["."] ".") + (escapeC (stringToCharacters " !\"#$%&'()*+,;<=>=@[\\]^`{|}~-") (if normalizedPath == "/" then normalizedPath else trim normalizedPath))); # Quotes an argument for use in Exec* service lines. @@ -62,12 +97,12 @@ rec { # substitution for the directive. escapeSystemdExecArg = arg: let - s = if builtins.isPath arg then "${arg}" - else if builtins.isString arg then arg - else if builtins.isInt arg || builtins.isFloat arg || lib.isDerivation arg then toString arg + s = if isPath arg then "${arg}" + else if isString arg then arg + else if isInt arg || isFloat arg || isDerivation arg then toString arg else throw "escapeSystemdExecArg only allows strings, paths, numbers and derivations"; in - replaceStrings [ "%" "$" ] [ "%%" "$$" ] (builtins.toJSON s); + replaceStrings [ "%" "$" ] [ "%%" "$$" ] (toJSON s); # Quotes a list of arguments into a single string for use in a Exec* # line. @@ -197,7 +232,7 @@ rec { (attrNames secrets)) + "\n" + "${pkgs.jq}/bin/jq >'${output}' " - + lib.escapeShellArg (stringOrDefault + + escapeShellArg (stringOrDefault (concatStringsSep " | " (imap1 (index: name: ''${name} = $ENV.secret${toString index}'') @@ -205,7 +240,7 @@ rec { ".") + '' <<'EOF' - ${builtins.toJSON set} + ${toJSON set} EOF (( ! $inherit_errexit_enabled )) && shopt -u inherit_errexit ''; @@ -222,9 +257,9 @@ rec { */ removePackagesByName = packages: packagesToRemove: let - namesToRemove = map lib.getName packagesToRemove; + namesToRemove = map getName packagesToRemove; in - lib.filter (x: !(builtins.elem (lib.getName x) namesToRemove)) packages; + filter (x: !(elem (getName x) namesToRemove)) packages; systemdUtils = { lib = import ./systemd-lib.nix { inherit lib config pkgs; };