From a942b33f04f2dc6a10023f86a3a1775eda9186c9 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 22 Jul 2017 23:44:23 +0200 Subject: shell: add get-source command --- shell.nix | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 5ea9ff3b..58b956c0 100644 --- a/shell.nix +++ b/shell.nix @@ -2,6 +2,10 @@ let lib = import ./lib; pkgs = import { overlays = [(import ./krebs/5pkgs)]; }; + # + # high level commands + # + # usage: deploy [--user=USER] --system=SYSTEM [--target=TARGET] cmds.deploy = pkgs.writeDash "cmds.deploy" '' set -efu @@ -29,6 +33,22 @@ let exec ${utils.build} config.system.build.toplevel ''; + # + # low level commands + # + + # usage: get-source SOURCE_FILE + cmds.get-source = pkgs.writeDash "cmds.get-source" '' + set -efu + exec ${pkgs.nix}/bin/nix-instantiate \ + --eval \ + --json \ + --readonly-mode \ + --show-trace \ + --strict \ + "$1" + ''; + init.args = pkgs.writeText "init.args" /* sh */ '' args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \ -o s:t:u: \ @@ -90,13 +110,7 @@ let }; populate = pkgs.writeDash "init.env.populate" '' set -efu - _source=$(${pkgs.nix}/bin/nix-instantiate \ - --eval \ - --json \ - --readonly-mode \ - --show-trace \ - --strict \ - "$source") + _source=$(get-source "$source") echo $_source | ${pkgs.populate}/bin/populate \ "$target_user@$target_host:$target_port$target_path" \ -- cgit v1.2.3 From 854311b004c947ec825f04df9f5fb8fda777e5dc Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 23 Jul 2017 00:04:54 +0200 Subject: shell: add parse-target command --- shell.nix | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 58b956c0..0a8d8dde 100644 --- a/shell.nix +++ b/shell.nix @@ -49,6 +49,24 @@ let "$1" ''; + # usage: parse-target [USER@]HOST[:PORT][/PATH] + cmds.parse-target = pkgs.writeDash "cmds.parse-target" '' + set -efu + script=${pkgs.writeText "cmds.parse-target.jq" '' + def when(c; f): if c then f else . end; + def capturesDef(i; v): .captures[i].string | when(. == null; v); + $target | match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | { + user: capturesDef(0; "root"), + host: capturesDef(1; env.system), + port: capturesDef(2; "22"), + path: capturesDef(3; "/var/src"), + } | . + { + local: (.user == env.LOGNAME and .host == env.HOSTNAME), + } + ''} + exec ${pkgs.jq}/bin/jq -enrf "$script" --arg target "$1" \ + ''; + init.args = pkgs.writeText "init.args" /* sh */ '' args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \ -o s:t:u: \ @@ -74,7 +92,7 @@ let export target export user - export target_object="$(${init.env.parsetarget} $target)" + export target_object="$(parse-target "$target")" export target_user="$(echo $target_object | ${pkgs.jq}/bin/jq -r .user)" export target_host="$(echo $target_object | ${pkgs.jq}/bin/jq -r .host)" export target_port="$(echo $target_object | ${pkgs.jq}/bin/jq -r .port)" @@ -88,26 +106,6 @@ let fi fi '' // { - parsetarget = pkgs.writeDash "init.env.parsetarget" '' - set -efu - exec ${pkgs.jq}/bin/jq \ - -enr \ - --arg target "$1" \ - -f ${init.env.parsetarget.jq} - '' // { - jq = pkgs.writeText "init.env.parsetarget.jq" '' - def when(c; f): if c then f else . end; - def capturesDef(i; v): .captures[i].string | when(. == null; v); - $target | match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | { - user: capturesDef(0; "root"), - host: capturesDef(1; env.system), - port: capturesDef(2; "22"), - path: capturesDef(3; "/var/src"), - } | . + { - local: (.user == env.LOGNAME and .host == env.HOSTNAME), - } - ''; - }; populate = pkgs.writeDash "init.env.populate" '' set -efu _source=$(get-source "$source") -- cgit v1.2.3 From 4b894507b2fef23e68097bd2b758acf151ab7993 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 23 Jul 2017 00:28:35 +0200 Subject: shell: add support for --pure --- shell.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 0a8d8dde..deca2ca3 100644 --- a/shell.nix +++ b/shell.nix @@ -174,7 +174,8 @@ let in pkgs.stdenv.mkDerivation { name = "stockholm"; shellHook = /* sh */ '' - export NIX_PATH="stockholm=$PWD''${NIX_PATH+:$NIX_PATH}" + export NIX_PATH=stockholm=$PWD:nixpkgs=${toString } + export NIX_REMOTE=daemon export PATH=${lib.makeBinPath [ shell.cmdspkg ]} -- cgit v1.2.3 From dc0ca967cb3399fd66e8d759ee6fc1211c1403de Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 23 Jul 2017 01:03:53 +0200 Subject: shell: add quote command --- shell.nix | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index deca2ca3..d40f6590 100644 --- a/shell.nix +++ b/shell.nix @@ -67,6 +67,18 @@ let exec ${pkgs.jq}/bin/jq -enrf "$script" --arg target "$1" \ ''; + # usage: quote [ARGS...] + cmds.quote = pkgs.writeDash "cmds.quote" '' + set -efu + prefix= + for x; do + y=$(${pkgs.jq}/bin/jq -nr --arg x "$x" '$x | @sh "\(.)"') + echo -n "$prefix$y" + prefix=' ' + done + echo + ''; + init.args = pkgs.writeText "init.args" /* sh */ '' args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \ -o s:t:u: \ @@ -117,21 +129,17 @@ let ''; proxy = pkgs.writeDash "init.env.proxy" '' set -efu - q() { - ${pkgs.jq}/bin/jq -nr --arg x "$*" '$x | @sh "\(.)"' - } exec ${pkgs.openssh}/bin/ssh \ "$target_user@$target_host" -p "$target_port" \ cd "$target_path/stockholm" \; \ - NIX_PATH=$(q "$target_path") \ - STOCKHOLM_VERSION=$STOCKHOLM_VERSION \ - nix-shell \ - --run $(q \ - system=$system \ - target=$target \ - using_proxy=true \ - "$*" - ) + NIX_PATH=$(quote "$target_path") \ + STOCKHOLM_VERSION=$(quote "$STOCKHOLM_VERSION") \ + nix-shell --run "$(quote " + system=$(quote "$system") \ + target=$(quote "$target") \ + using_proxy=true \ + $(quote "$@") + ")" ''; }; -- cgit v1.2.3 From 13d1fe582d2c9e74c0755f0c3e7c05b6e5041a31 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 23 Jul 2017 01:33:58 +0200 Subject: shell: add --default=TARGET option to parse-target --- shell.nix | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index d40f6590..2973d4c5 100644 --- a/shell.nix +++ b/shell.nix @@ -49,22 +49,39 @@ let "$1" ''; - # usage: parse-target [USER@]HOST[:PORT][/PATH] + # usage: parse-target [--default=TARGET] TARGET + # TARGET = [USER@]HOST[:PORT][/PATH] cmds.parse-target = pkgs.writeDash "cmds.parse-target" '' set -efu - script=${pkgs.writeText "cmds.parse-target.jq" '' - def when(c; f): if c then f else . end; - def capturesDef(i; v): .captures[i].string | when(. == null; v); - $target | match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | { - user: capturesDef(0; "root"), - host: capturesDef(1; env.system), - port: capturesDef(2; "22"), - path: capturesDef(3; "/var/src"), - } | . + { - local: (.user == env.LOGNAME and .host == env.HOSTNAME), - } - ''} - exec ${pkgs.jq}/bin/jq -enrf "$script" --arg target "$1" \ + args=$(${pkgs.utillinux}/bin/getopt -n "$0" -s sh \ + -o d: \ + -l default: \ + -- "$@") + if \test $? != 0; then exit 1; fi + eval set -- "$args" + default_target= + while :; do case $1 in + -d|--default) default_target=$2; shift 2;; + --) shift; break;; + esac; done + target=$1; shift + for arg; do echo "$0: bad argument: $arg" >&2; done + if \test $# != 0; then exit 2; fi + exec ${pkgs.jq}/bin/jq \ + -enr \ + --arg default_target "$default_target" \ + --arg target "$target" \ + -f ${pkgs.writeText "cmds.parse-target.jq" '' + def parse: match("^(?:([^@]+)@)?([^:/]+)?(?::([0-9]+))?(/.*)?$") | { + user: .captures[0].string, + host: .captures[1].string, + port: .captures[2].string, + path: .captures[3].string, + }; + def sanitize: with_entries(select(.value != null)); + ($default_target | parse) + ($target | parse | sanitize) | + . + { local: (.user == env.LOGNAME and .host == env.HOSTNAME) } + ''} ''; # usage: quote [ARGS...] @@ -104,7 +121,9 @@ let export target export user - export target_object="$(parse-target "$target")" + default_target=root@$system:22/var/src + + export target_object="$(parse-target "$target" -d "$default_target")" export target_user="$(echo $target_object | ${pkgs.jq}/bin/jq -r .user)" export target_host="$(echo $target_object | ${pkgs.jq}/bin/jq -r .host)" export target_port="$(echo $target_object | ${pkgs.jq}/bin/jq -r .port)" -- cgit v1.2.3