Avoid top-level with ...; in nixos/lib/utils.nix

This commit is contained in:
Philip Taron 2024-03-27 22:02:32 -07:00
parent ad920b32c2
commit 546fc67242
No known key found for this signature in database

View file

@ -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 { rec {
# Copy configuration files to avoid having the entire sources in the system closure # 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 cp ${filePath} $out
''; '';
@ -46,11 +81,11 @@ rec {
escapeSystemdPath = s: let escapeSystemdPath = s: let
replacePrefix = p: r: s: (if (hasPrefix p s) then r + (removePrefix p s) else s); replacePrefix = p: r: s: (if (hasPrefix p s) then r + (removePrefix p s) else s);
trim = s: removeSuffix "/" (removePrefix "/" s); trim = s: removeSuffix "/" (removePrefix "/" s);
normalizedPath = strings.normalizePath s; normalizedPath = normalizePath s;
in in
replaceStrings ["/"] ["-"] replaceStrings ["/"] ["-"]
(replacePrefix "." (strings.escapeC ["."] ".") (replacePrefix "." (escapeC ["."] ".")
(strings.escapeC (stringToCharacters " !\"#$%&'()*+,;<=>=@[\\]^`{|}~-") (escapeC (stringToCharacters " !\"#$%&'()*+,;<=>=@[\\]^`{|}~-")
(if normalizedPath == "/" then normalizedPath else trim normalizedPath))); (if normalizedPath == "/" then normalizedPath else trim normalizedPath)));
# Quotes an argument for use in Exec* service lines. # Quotes an argument for use in Exec* service lines.
@ -62,12 +97,12 @@ rec {
# substitution for the directive. # substitution for the directive.
escapeSystemdExecArg = arg: escapeSystemdExecArg = arg:
let let
s = if builtins.isPath arg then "${arg}" s = if isPath arg then "${arg}"
else if builtins.isString arg then arg else if isString arg then arg
else if builtins.isInt arg || builtins.isFloat arg || lib.isDerivation arg then toString arg else if isInt arg || isFloat arg || isDerivation arg then toString arg
else throw "escapeSystemdExecArg only allows strings, paths, numbers and derivations"; else throw "escapeSystemdExecArg only allows strings, paths, numbers and derivations";
in in
replaceStrings [ "%" "$" ] [ "%%" "$$" ] (builtins.toJSON s); replaceStrings [ "%" "$" ] [ "%%" "$$" ] (toJSON s);
# Quotes a list of arguments into a single string for use in a Exec* # Quotes a list of arguments into a single string for use in a Exec*
# line. # line.
@ -197,7 +232,7 @@ rec {
(attrNames secrets)) (attrNames secrets))
+ "\n" + "\n"
+ "${pkgs.jq}/bin/jq >'${output}' " + "${pkgs.jq}/bin/jq >'${output}' "
+ lib.escapeShellArg (stringOrDefault + escapeShellArg (stringOrDefault
(concatStringsSep (concatStringsSep
" | " " | "
(imap1 (index: name: ''${name} = $ENV.secret${toString index}'') (imap1 (index: name: ''${name} = $ENV.secret${toString index}'')
@ -205,7 +240,7 @@ rec {
".") ".")
+ '' + ''
<<'EOF' <<'EOF'
${builtins.toJSON set} ${toJSON set}
EOF EOF
(( ! $inherit_errexit_enabled )) && shopt -u inherit_errexit (( ! $inherit_errexit_enabled )) && shopt -u inherit_errexit
''; '';
@ -222,9 +257,9 @@ rec {
*/ */
removePackagesByName = packages: packagesToRemove: removePackagesByName = packages: packagesToRemove:
let let
namesToRemove = map lib.getName packagesToRemove; namesToRemove = map getName packagesToRemove;
in in
lib.filter (x: !(builtins.elem (lib.getName x) namesToRemove)) packages; filter (x: !(elem (getName x) namesToRemove)) packages;
systemdUtils = { systemdUtils = {
lib = import ./systemd-lib.nix { inherit lib config pkgs; }; lib = import ./systemd-lib.nix { inherit lib config pkgs; };