shell: use withGetopt

This commit is contained in:
tv 2017-09-15 00:33:34 +02:00
parent ab36abc933
commit b28dabfc07

118
shell.nix
View File

@ -6,43 +6,38 @@ let
# high level commands # high level commands
# #
# usage: deploy cmds.deploy = pkgs.withGetopt {
# [--force-populate] force-populate = { default = /* sh */ "false"; switch = true; };
# [--quiet] quiet = { default = /* sh */ "false"; switch = true; };
# [--source=PATH] source_file = {
# --system=SYSTEM default = /* sh */ "$user/1systems/$system/source.nix";
# [--target=TARGET] long = "source";
# [--user=USER] };
cmds.deploy = pkgs.writeDash "cmds.deploy" '' system = {};
target.default = /* sh */ "$system";
user.default = /* sh */ "$LOGNAME";
} (opts: pkgs.writeDash "cmds.deploy" ''
set -efu set -efu
command=deploy
. ${init.args}
\test -n "''${quiet-}" || quiet=false
\test -n "''${target-}" || target=$system
\test -n "''${user-}" || user=$LOGNAME
\test -n "''${source_file}" || source_file=$user/1systems/$system/source.nix
. ${init.env} . ${init.env}
. ${init.proxy} . ${init.proxy opts}
exec ${utils.deploy} exec ${utils.deploy}
''; '');
# usage: install cmds.install = pkgs.withGetopt {
# [--force-populate] force-populate = { default = /* sh */ "false"; switch = true; };
# [--quiet] quiet = { default = /* sh */ "false"; switch = true; };
# [--source=PATH] source_file = {
# --system=SYSTEM default = /* sh */ "$user/1systems/$system/source.nix";
# --target=TARGET long = "source";
# [--user=USER] };
cmds.install = pkgs.writeBash "cmds.install" '' system = {};
target = {};
user.default = /* sh */ "$LOGNAME";
} (opts: pkgs.writeBash "cmds.install" ''
set -efu set -efu
command=install
. ${init.args}
\test -n "''${quiet-}" || quiet=false
\test -n "''${user-}" || user=$LOGNAME
\test -n "''${source_file}" || source_file=$user/1systems/$system/source.nix
. ${init.env} . ${init.env}
if \test "''${using_proxy-}" != true; then if \test "''${using_proxy-}" != true; then
@ -55,7 +50,7 @@ let
# TODO inline prepare.sh? # TODO inline prepare.sh?
fi fi
. ${init.proxy} . ${init.proxy opts}
# Reset PATH because we need access to nixos-install. # Reset PATH because we need access to nixos-install.
# TODO provide nixos-install instead of relying on prepare.sh # TODO provide nixos-install instead of relying on prepare.sh
@ -75,30 +70,28 @@ let
cd cd
exec nixos-install exec nixos-install
''; '');
# usage: test cmds.test = pkgs.withGetopt {
# [--force-populate] force-populate = { default = /* sh */ "false"; switch = true; };
# [--quiet] quiet = { default = /* sh */ "false"; switch = true; };
# [--source=PATH] source_file = {
# --system=SYSTEM default = /* sh */ "$user/1systems/$system/source.nix";
# --target=TARGET long = "source";
# [--user=USER] };
cmds.test = pkgs.writeDash "cmds.test" /* sh */ '' system = {};
target = {};
user.default = /* sh */ "$LOGNAME";
} (opts: pkgs.writeDash "cmds.test" /* sh */ ''
set -efu set -efu
export dummy_secrets=true export dummy_secrets=true
command=test
. ${init.args}
\test -n "''${quiet-}" || quiet=false
\test -n "''${user-}" || user=$LOGNAME
\test -n "''${source_file}" || source_file=$user/1systems/$system/source.nix
. ${init.env} . ${init.env}
. ${init.proxy} . ${init.proxy opts}
exec ${utils.build} config.system.build.toplevel exec ${utils.build} config.system.build.toplevel
''; '');
# #
# low level commands # low level commands
@ -163,28 +156,6 @@ let
echo echo
''; '';
init.args = pkgs.writeText "init.args" /* sh */ ''
args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \
-o Qs:t:u: \
-l force-populate,quiet,source:,system:,target:,user: \
-- "$@")
if \test $? != 0; then exit 1; fi
eval set -- "$args"
force_populate=false
source_file=
while :; do case $1 in
--force-populate) force_populate=true; shift;;
-Q|--quiet) quiet=true; shift;;
--source) source_file=$2; shift 2;;
-s|--system) system=$2; shift 2;;
-t|--target) target=$2; shift 2;;
-u|--user) user=$2; shift 2;;
--) shift; break;;
esac; done
for arg; do echo "$command: bad argument: $arg" >&2; done
if \test $# != 0; then exit 2; fi
'';
init.env = pkgs.writeText "init.env" /* sh */ '' init.env = pkgs.writeText "init.env" /* sh */ ''
export quiet export quiet
export system export system
@ -201,7 +172,7 @@ let
export target_local="$(echo $target_object | ${pkgs.jq}/bin/jq -r .local)" export target_local="$(echo $target_object | ${pkgs.jq}/bin/jq -r .local)"
''; '';
init.proxy = pkgs.writeText "init.proxy" /* sh */ '' init.proxy = opts: pkgs.writeText "init.proxy" /* sh */ ''
if \test "''${using_proxy-}" != true; then if \test "''${using_proxy-}" != true; then
source=$(get-source "$source_file") source=$(get-source "$source_file")
@ -219,11 +190,12 @@ let
NIX_PATH=$(quote "$target_path") \ NIX_PATH=$(quote "$target_path") \
STOCKHOLM_VERSION=$(quote "$STOCKHOLM_VERSION") \ STOCKHOLM_VERSION=$(quote "$STOCKHOLM_VERSION") \
nix-shell --run "$(quote " nix-shell --run "$(quote "
quiet=$(quote "$quiet") \ ${lib.concatStringsSep " " (lib.mapAttrsToList
system=$(quote "$system") \ (name: opt: /* sh */ "${opt.varname}=\$(quote ${opt.ref})")
target=$(quote "$target") \ opts
)} \
using_proxy=true \ using_proxy=true \
$(quote "$command" "$@") $(quote "$0" "$@")
")" ")"
fi fi
fi fi