From 6f44532be5e2561f2772d056bdaeba6c39ecca9f Mon Sep 17 00:00:00 2001 From: Niklas Gollenstede Date: Mon, 4 Dec 2023 19:30:01 +0100 Subject: [PATCH] (re-)move generic-arg-*, more concise default.nix --- flake.lock | Bin 1669 -> 1669 bytes lib/default.nix | 7 ++-- lib/nixos.nix | 5 ++- lib/setup-scripts/utils.sh | 66 -------------------------------- modules/bootloader/default.nix | 2 +- modules/default.nix | 2 +- modules/filesystems/default.nix | 2 +- modules/setup/default.nix | 2 +- modules/vm-exec.nix.md | 2 +- overlays/default.nix | 2 +- patches/default.nix | 2 +- 11 files changed, 14 insertions(+), 78 deletions(-) diff --git a/flake.lock b/flake.lock index 269e5e5317ec6cbf16069c623853e538a9aeaec6..5b68ff35064e607200aadc4dc86c634f4dfe27c7 100644 GIT binary patch delta 358 zcmXZWJ5B;Y007XSEFH0w#y~8T1mn)mcZb;E5)c9c{`hpW^9h2i$nx_5G*%{a5DKp0 zExd??c!Kvf=}r23k-`XgnAq*nS)tykRb*vQE4z#COwC?vQ7<-&(z^au6`gV1Az?G_ zw>^I~TG?$fV^5AR>~A7-J=4qcV(y%40Hq?FKp=^b94Mhf?lS`gqFhlyB$Pty zYP6hBd&AH5nl#a~+r0 delta 358 zcmW;FJ5B;Y7y#g)Ku0Xa!bm8ThzaxBS(ezyqhJM*RRqd^W@iK!Hw40C>1nJ?<{naT z4R7H^TsXj&Z=2a>z87iAY>RTUJGs22sE#9V1Jhn_Hm!+Ozqp>m27BVQ^%?3-``sO> zq5UjX!~7&K%-QL+`AcL=GqucDa(M|L9Aa~fY_5Q7!n8#R0c9yI9f}ZeR<;Q?xm0!k zmAFd8wZ$<#)I}6@c@#eIzEoX?tuS14T%kH{TUEzF+531S>&FPjk^2&uT{0WrwE~qG zh@*vM$BiKElQ^~=i3K1;G$F#5a0uHmxHosHbMu?Zn44rJO*m!TI^L6~L9aOpbX1R~ zPG?Q(O(YyqT9d|(^_Rn%upIX+T? J?sF-X_y^WmYoh=F diff --git a/lib/default.nix b/lib/default.nix index 10599f7..6e49ee0 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,4 +1,3 @@ -dirname: inputs@{ nixpkgs, functions, ...}: let - categories = functions.lib.importAll inputs dirname; - self = (builtins.foldl' (a: b: a // (if builtins.isAttrs b then b else { })) { } (builtins.attrValues (builtins.removeAttrs categories [ "setup-scripts" ]))) // categories; -in self // { __internal__ = nixpkgs.lib // { self = self; fun = functions.lib; }; } +dirname: inputs: inputs.functions.lib.importLib inputs dirname { rename = { + functions = "fun"; +}; noSpread = [ "setup-scripts" ]; } diff --git a/lib/nixos.nix b/lib/nixos.nix index 1b685a3..0b7302b 100644 --- a/lib/nixos.nix +++ b/lib/nixos.nix @@ -235,7 +235,7 @@ in rec { # provide installer tools (not necessarily for system.pkgs.config.hostPlatform) hostPath=$PATH ; PATH=${lib.makeBinPath tools} - ${extractBashFunction (builtins.readFile setup-scripts.utils) "generic-arg-parse"} + source ${inputs.functions.lib.bash.generic-arg-parse} set -o pipefail -o nounset # (do not rely on errexit) generic-arg-parse "$@" || exit @@ -257,6 +257,9 @@ in rec { declare-flag '*' trace "" "Turn on bash's »errtrace« option before running »COMMAND«." declare-flag '*' quiet "" "Try to suppress all non-error output. May also swallow some error related output." declare -g -A allowedCommands=( ) ; function declare-command { allowedCommands[$@]=$(< /dev/stdin) ; } + source ${inputs.functions.lib.bash.generic-arg-verify} + source ${inputs.functions.lib.bash.generic-arg-help} + source ${inputs.functions.lib.bash.prepend_trap} ${system.config.${installer}.build.scripts { native = pkgs; }} if [[ ''${args[help]:-} ]] ; then ( functionDoc= ; while IFS= read -u3 -r name ; do diff --git a/lib/setup-scripts/utils.sh b/lib/setup-scripts/utils.sh index edc1e00..1da4b6e 100644 --- a/lib/setup-scripts/utils.sh +++ b/lib/setup-scripts/utils.sh @@ -3,72 +3,6 @@ # Utilities ## -## Performs a simple and generic parsing of CLI arguments. Creates a global associative array »args« and a global normal array »argv«. -# Named options may be passed as »--name[=value]«, where »value« defaults to »1«, and are assigned to »args«. -# Everything else, or everything following the »--« argument, ends up as positional arguments in »argv«. -# Checking the validity of the parsed arguments is up to the caller. -function generic-arg-parse { # ... - declare -g -A args=( ) ; declare -g -a argv=( ) # this ends up in the caller's scope - while (( "$#" )) ; do - if [[ $1 == -- ]] ; then shift ; argv+=( "$@" ) ; \return 0 ; fi - if [[ $1 == --* ]] ; then - if [[ $1 == *=* ]] ; then - local key=${1/=*/} ; args[${key/--/}]=${1/$key=/} - else args[${1/--/}]=1 ; fi - else argv+=( "$1" ) ; fi - shift ; done -} - -## Shows the help text for a program and exits, if »--help« was passed as argument and parsed, or does nothing otherwise. -# Expects to be called between parsing and verifying the arguments. -# Uses »allowedArgs« for the list of the named arguments (the values are the descriptions). -# »name« should be the program name/path (usually »$0«), »args« the form/names of any positional arguments expected (e.g. »SOURCE... DEST«) and is included in the "Usage" description, -# »description« the introductory text shown before the "Usage", and »suffix« any text printed after the argument list. -function generic-arg-help { # 1: name, 2?: args, 3?: description, 4?: suffix, 5?: usageLine - if [[ ! ${args[help]:-} ]] ; then : ${allowedArgs[help]:=1} ; \return 0 ; fi - [[ ! ${3:-} ]] || echo "$3" - printf "${5:-'Usage:\n %s [FLAG[=value]]... [--] %s\n\nWhere »FLAG« may be any of:\n'}" "$1" "${2:-}" - local name ; while IFS= read -u3 -r name ; do - printf ' %s\n %s\n' "$name" "${allowedArgs[$name]//$'\n'/$'\n '}" - done 3< <( printf '%s\n' "${!allowedArgs[@]}" | LC_ALL=C sort ) - printf ' %s\n %s\n' "--help" "Do nothing but print this message and exit with success." - [[ ! ${4:-} ]] || echo "$4" - \exit 0 -} - -## Performs a basic verification of the named arguments passed by the user and parsed by »generic-arg-parse« against the names in »allowedArgs«. -# Entries in »allowedArgs« should have the form »[--name]="description"« for boolean flags, and »[--name=VAL]="description"« for string arguments. -# »description« is used by »generic-arg-help«. Boolean flags may only have the values »1« (as set by »generic-ags-parse« for flags without value) or be empty. -# »VAL« is purely nominal. Any argument passed that is not in »allowedArgs« raises an error. -function generic-arg-verify { # 1?: exitCode - local exitCode=${exitCode:-${1:-1}} - local names=' '"${!allowedArgs[@]}" - for name in "${!args[@]}" ; do - if [[ ${allowedArgs[--$name]:-} ]] ; then - if [[ ${args[$name]} == '' || ${args[$name]} == 1 ]] ; then continue ; fi - echo "Argument »--$name« should be a boolean, but its value is: ${args[$name]}" 1>&2 ; \return $exitCode - fi - if [[ $names == *' --'"$name"'='* || $names == *' --'"$name"'[='* ]] ; then continue ; fi - if [[ ${undeclared:-} && $name =~ $undeclared ]] ; then continue ; fi - echo "Unexpected argument »--$name«.${allowedArgs[help]:+ Call with »--help« for a list of valid arguments.}" 1>&2 ; \return $exitCode - done -} - -## Prepends a command to a trap. Especially useful fo define »finally« commands via »prepend_trap '' EXIT«. -# NOTE: When calling this in a sub-shell whose parents already has traps installed, make sure to do »trap - trapName« first. On a new shell, this should be a no-op, but without it, the parent shell's traps will be added to the sub-shell as well (due to strange behavior of »trap -p« (in bash ~5.1.8)). -function prepend_trap { # 1: command, ...: trapNames - fatal() { printf "ERROR: $@\n" 1>&2 ; \return 1 ; } - local cmd=$1 ; shift 1 || fatal "${FUNCNAME} usage error" - local name ; for name in "$@" ; do - trap -- "$( set +x - printf '%s\n' "( ${cmd} ) || true ; " - p3() { printf '%s\n' "${3:-}" ; } - eval "p3 $(trap -p "${name}")" - )" "${name}" || fatal "unable to add to trap ${name}" - done -} -declare -f -t prepend_trap # required to modify DEBUG or RETURN traps - ## Given the name to an existing bash function, this creates a copy of that function with a new name (in the current scope). function copy-function { # 1: existingName, 2: newName local original=$(declare -f "${1?existingName not provided}") ; if [[ ! $original ]] ; then echo "Function $1 is not defined" 1>&2 ; \return 1 ; fi diff --git a/modules/bootloader/default.nix b/modules/bootloader/default.nix index 2c668d7..5be180e 100644 --- a/modules/bootloader/default.nix +++ b/modules/bootloader/default.nix @@ -1 +1 @@ -dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { } +dirname: inputs: inputs.functions.lib.importModules inputs dirname { } diff --git a/modules/default.nix b/modules/default.nix index 2c668d7..5be180e 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -1 +1 @@ -dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { } +dirname: inputs: inputs.functions.lib.importModules inputs dirname { } diff --git a/modules/filesystems/default.nix b/modules/filesystems/default.nix index 2c668d7..5be180e 100644 --- a/modules/filesystems/default.nix +++ b/modules/filesystems/default.nix @@ -1 +1 @@ -dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { } +dirname: inputs: inputs.functions.lib.importModules inputs dirname { } diff --git a/modules/setup/default.nix b/modules/setup/default.nix index 2c668d7..5be180e 100644 --- a/modules/setup/default.nix +++ b/modules/setup/default.nix @@ -1 +1 @@ -dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importModules inputs dirname { } +dirname: inputs: inputs.functions.lib.importModules inputs dirname { } diff --git a/modules/vm-exec.nix.md b/modules/vm-exec.nix.md index ffa442a..ca87da8 100644 --- a/modules/vm-exec.nix.md +++ b/modules/vm-exec.nix.md @@ -52,7 +52,7 @@ in let hostModule = { mkdir -p $out/bin ln -s ${cfg.system.build.toplevel} $out/system ln -s ${pkgs.writeShellScript name '' - ${lib.fun.extractBashFunction (builtins.readFile lib.self.setup-scripts.utils) "generic-arg-parse"} + source ${lib.fun.bash.generic-arg-parse} generic-arg-parse "$@" ; set -- ; set -o pipefail -u #; set -x script=''${argv[0]:?'The first positional argument must be the script to execute in the VM'} ; argv=( "''${argv[@]:1}" ) diff --git a/overlays/default.nix b/overlays/default.nix index 63c4af9..d191c0a 100644 --- a/overlays/default.nix +++ b/overlays/default.nix @@ -1 +1 @@ -dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importOverlays inputs dirname { } +dirname: inputs: inputs.functions.lib.importOverlays inputs dirname { } diff --git a/patches/default.nix b/patches/default.nix index 277021b..a6aab07 100644 --- a/patches/default.nix +++ b/patches/default.nix @@ -1 +1 @@ -dirname: inputs@{ self, nixpkgs, ...}: self.lib.__internal__.fun.importPatches inputs dirname { } +dirname: inputs: inputs.functions.lib.importPatches inputs dirname { }