From ed71d568831941632d2fa6e783d10d703c4ceee6 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 29 Jun 2017 22:06:13 +0200 Subject: shell: init --- shell.nix | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 shell.nix (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix new file mode 100644 index 00000000..d6e09e1a --- /dev/null +++ b/shell.nix @@ -0,0 +1,210 @@ +{ nixpkgs ? import {} }: let + + inherit (nixpkgs) lib pkgs; + slib = import ./lib; + spkgs = { + populate = pkgs.callPackage ./krebs/5pkgs/simple/populate {}; + whatsupnix = pkgs.callPackage ./krebs/5pkgs/simple/whatsupnix {}; + }; + + # usage: deploy system=SYSTEM [target=TARGET] + cmds.deploy = pkgs.writeScript "cmds.deploy" /* sh */ '' + #! ${pkgs.dash}/bin/dash + set -efu + + command=deploy + . ${init.args} + \test -n "''${target-}" || target=$system + . ${init.env} + + exec ${utils.deploy} + ''; + + # usage: test system=SYSTEM target=TARGET + cmds.test = pkgs.writeScript "cmds.test" /* sh */ '' + #! ${pkgs.dash}/bin/dash + set -efu + + command=test + . ${init.args} + . ${init.env} + + export dummy_secrets=true + exec ${utils.build} config.system.build.toplevel + ''; + + init.args = pkgs.writeText "init.args" /* sh */ '' + fail= + for arg; do + case $arg in + system=*) system=''${arg#*=};; + target=*) target=''${arg#*=};; + *) echo "$command: bad argument: $arg" >&2; fail=1 + esac + done + if \test -n "$fail"; then + exit 1 + fi + unset fail + ''; + + init.env = pkgs.writeText "init.env" /* sh */ '' + config=''${config-$LOGNAME/1systems/$system.nix} + + export config + export system + export target + + export target_object="$(${init.env.parsetarget} $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)" + export target_path="$(echo $target_object | ${pkgs.jq}/bin/jq -r .path)" + export target_local="$(echo $target_object | ${pkgs.jq}/bin/jq -r .local)" + + export qtarget="$target_user@$target_host:$target_port$target_path" + + ${init.env.populate} + + if \test "$target_local" != true && \test "''${DISABLE_PROXY-}" != 1; then + exec ${init.env.proxy} "$command" "$@" + fi + '' // { + parsetarget = pkgs.writeScript "init.env.parsetarget" /* sh */ '' + #! ${pkgs.dash}/bin/dash + 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.writeScript "init.env.populate" /* sh */ '' + #! ${pkgs.dash}/bin/dash + set -efu + if \test "''${DISABLE_POPULATE-}" = 1; then + exit + fi + set -x + ${pkgs.nix}/bin/nix-instantiate \ + --eval \ + --json \ + --readonly-mode \ + --show-trace \ + --strict \ + -I nixos-config="$config" \ + -E 'with import ; config.krebs.build.source' \ + | + ${spkgs.populate}/bin/populate "$qtarget" >&2 + ''; + proxy = pkgs.writeScript "init.env.proxy" /* sh */ '' + #! ${pkgs.dash}/bin/dash + 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 \ + --command $(q \ + config=$config \ + system=$system \ + target=$target \ + DISABLE_POPULATE=1 \ + DISABLE_PROXY=1 \ + "$*" + ) + ''; + }; + + utils.build = pkgs.writeScript "utils.build" /* sh */ '' + #! ${pkgs.dash}/bin/dash + set -efu + expr=$1 + shift + ${pkgs.nix}/bin/nix-build \ + -Q \ + --no-out-link \ + --show-trace \ + -E "with import ; $expr" \ + -I "$target_path" \ + "$@" \ + 2>&1 | + ${pkgs.coreutils}/bin/stdbuf -oL ${spkgs.whatsupnix}/bin/whatsupnix + ''; + + utils.deploy = pkgs.writeScript "utils.deploy" /* sh */ '' + #! ${pkgs.dash}/bin/dash + set -efu + PATH=/run/current-system/sw/bin nixos-rebuild \ + switch \ + -Q \ + --show-trace \ + -I "$target_path" \ + "$@" \ + 2>&1 | + ${pkgs.coreutils}/bin/stdbuf -oL ${spkgs.whatsupnix}/bin/whatsupnix + ''; + + hook.get-version = pkgs.writeScript "hook.get-version" /* sh */ '' + #! ${pkgs.dash}/bin/dash + set -efu + version=git.$(${pkgs.git}/bin/git describe --always --dirty) + case $version in (*-dirty) + version=$version@$HOSTNAME + esac + date=$(${pkgs.coreutils}/bin/date +%y.%m) + echo "$date.$version" + ''; + + hook.pkg = pkgs.runCommand "hook.pkg" {} /* sh */ '' + mkdir -p $out/bin + ${lib.concatStrings (lib.mapAttrsToList (name: path: /* sh */ '' + ln -s ${path} $out/bin/${name} + '') cmds)} + ''; + +in pkgs.stdenv.mkDerivation { + name = "stockholm"; + shellHook = '' + export NIX_PATH="stockholm=$PWD''${NIX_PATH+:$NIX_PATH}" + export PATH=${lib.makeBinPath [ + hook.pkg + ]} + + eval "$(declare -F | ${pkgs.gnused}/bin/sed s/declare/unset/)" + shopt -u no_empty_cmd_completion + unalias -a + + enable -n \ + . [ alias bg bind break builtin caller cd command compgen complete \ + compopt continue dirs disown eval exec false fc fg getopts hash \ + help history jobs kill let local logout mapfile popd printf pushd \ + pwd read readarray readonly shift source suspend test times trap \ + true typeset ulimit umask unalias wait + + exitHandler() { + : + } + + export HOSTNAME="$(${pkgs.nettools}/bin/hostname)" + export STOCKHOLM_VERSION="''${STOCKHOLM_VERSION-$(${hook.get-version})}" + + PS1='\[\e[38;5;162m\]\w\[\e[0m\] ' + ''; +} -- cgit v1.2.3 From b9c11e44e779769ca0a6b13eedee82782bc63927 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 03:16:24 +0200 Subject: shell: mark shellHook as sh --- shell.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index d6e09e1a..26db52c3 100644 --- a/shell.nix +++ b/shell.nix @@ -181,7 +181,7 @@ in pkgs.stdenv.mkDerivation { name = "stockholm"; - shellHook = '' + shellHook = /* sh */ '' export NIX_PATH="stockholm=$PWD''${NIX_PATH+:$NIX_PATH}" export PATH=${lib.makeBinPath [ hook.pkg -- cgit v1.2.3 From 849d0ba98f03dc8700e6b9bb724b9afdbde68713 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 04:27:02 +0200 Subject: shell: using proxy implies populated target --- shell.nix | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 26db52c3..9cc74066 100644 --- a/shell.nix +++ b/shell.nix @@ -64,10 +64,11 @@ export qtarget="$target_user@$target_host:$target_port$target_path" - ${init.env.populate} - - if \test "$target_local" != true && \test "''${DISABLE_PROXY-}" != 1; then - exec ${init.env.proxy} "$command" "$@" + if \test "''${using_proxy-}" != true; then + ${init.env.populate} + if \test "$target_local" != true; then + exec ${init.env.proxy} "$command" "$@" + fi fi '' // { parsetarget = pkgs.writeScript "init.env.parsetarget" /* sh */ '' @@ -94,10 +95,6 @@ populate = pkgs.writeScript "init.env.populate" /* sh */ '' #! ${pkgs.dash}/bin/dash set -efu - if \test "''${DISABLE_POPULATE-}" = 1; then - exit - fi - set -x ${pkgs.nix}/bin/nix-instantiate \ --eval \ --json \ @@ -125,8 +122,7 @@ config=$config \ system=$system \ target=$target \ - DISABLE_POPULATE=1 \ - DISABLE_PROXY=1 \ + using_proxy=true \ "$*" ) ''; -- cgit v1.2.3 From 7d983f09c8433b623ffb49435807d3417a93776b Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 04:27:25 +0200 Subject: shell: inline qtarget --- shell.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 9cc74066..e16f172e 100644 --- a/shell.nix +++ b/shell.nix @@ -62,8 +62,6 @@ export target_path="$(echo $target_object | ${pkgs.jq}/bin/jq -r .path)" export target_local="$(echo $target_object | ${pkgs.jq}/bin/jq -r .local)" - export qtarget="$target_user@$target_host:$target_port$target_path" - if \test "''${using_proxy-}" != true; then ${init.env.populate} if \test "$target_local" != true; then @@ -104,7 +102,9 @@ -I nixos-config="$config" \ -E 'with import ; config.krebs.build.source' \ | - ${spkgs.populate}/bin/populate "$qtarget" >&2 + ${spkgs.populate}/bin/populate \ + "$target_user@$target_host:$target_port$target_path" \ + >&2 ''; proxy = pkgs.writeScript "init.env.proxy" /* sh */ '' #! ${pkgs.dash}/bin/dash -- cgit v1.2.3 From 74522bba94a3a62dcaed15c6ab9ff4e064a654ab Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 04:40:45 +0200 Subject: shell: use stockholm overlay --- shell.nix | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index e16f172e..d8f3184d 100644 --- a/shell.nix +++ b/shell.nix @@ -1,11 +1,7 @@ -{ nixpkgs ? import {} }: let +{ nixpkgs ? import { overlays = [(import ./krebs/5pkgs)]; } }: let inherit (nixpkgs) lib pkgs; slib = import ./lib; - spkgs = { - populate = pkgs.callPackage ./krebs/5pkgs/simple/populate {}; - whatsupnix = pkgs.callPackage ./krebs/5pkgs/simple/whatsupnix {}; - }; # usage: deploy system=SYSTEM [target=TARGET] cmds.deploy = pkgs.writeScript "cmds.deploy" /* sh */ '' @@ -102,7 +98,7 @@ -I nixos-config="$config" \ -E 'with import ; config.krebs.build.source' \ | - ${spkgs.populate}/bin/populate \ + ${pkgs.populate}/bin/populate \ "$target_user@$target_host:$target_port$target_path" \ >&2 ''; @@ -141,7 +137,7 @@ -I "$target_path" \ "$@" \ 2>&1 | - ${pkgs.coreutils}/bin/stdbuf -oL ${spkgs.whatsupnix}/bin/whatsupnix + ${pkgs.coreutils}/bin/stdbuf -oL ${pkgs.whatsupnix}/bin/whatsupnix ''; utils.deploy = pkgs.writeScript "utils.deploy" /* sh */ '' @@ -154,7 +150,7 @@ -I "$target_path" \ "$@" \ 2>&1 | - ${pkgs.coreutils}/bin/stdbuf -oL ${spkgs.whatsupnix}/bin/whatsupnix + ${pkgs.coreutils}/bin/stdbuf -oL ${pkgs.whatsupnix}/bin/whatsupnix ''; hook.get-version = pkgs.writeScript "hook.get-version" /* sh */ '' -- cgit v1.2.3 From dbe4cc21e33ccc22b9dd352dd8c6913d60a0cbc0 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 04:53:34 +0200 Subject: pkgs.whatsupnix: print gawk output ASAP --- shell.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index d8f3184d..302429a4 100644 --- a/shell.nix +++ b/shell.nix @@ -137,7 +137,7 @@ -I "$target_path" \ "$@" \ 2>&1 | - ${pkgs.coreutils}/bin/stdbuf -oL ${pkgs.whatsupnix}/bin/whatsupnix + ${pkgs.whatsupnix}/bin/whatsupnix ''; utils.deploy = pkgs.writeScript "utils.deploy" /* sh */ '' @@ -150,7 +150,7 @@ -I "$target_path" \ "$@" \ 2>&1 | - ${pkgs.coreutils}/bin/stdbuf -oL ${pkgs.whatsupnix}/bin/whatsupnix + ${pkgs.whatsupnix}/bin/whatsupnix ''; hook.get-version = pkgs.writeScript "hook.get-version" /* sh */ '' -- cgit v1.2.3 From a2ea8740a40d5e9f5dc8e86d33e605a78963fc2f Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 04:56:21 +0200 Subject: shell: use writeDash --- shell.nix | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 302429a4..d6d21586 100644 --- a/shell.nix +++ b/shell.nix @@ -4,8 +4,7 @@ slib = import ./lib; # usage: deploy system=SYSTEM [target=TARGET] - cmds.deploy = pkgs.writeScript "cmds.deploy" /* sh */ '' - #! ${pkgs.dash}/bin/dash + cmds.deploy = pkgs.writeDash "cmds.deploy" '' set -efu command=deploy @@ -17,8 +16,7 @@ ''; # usage: test system=SYSTEM target=TARGET - cmds.test = pkgs.writeScript "cmds.test" /* sh */ '' - #! ${pkgs.dash}/bin/dash + cmds.test = pkgs.writeDash "cmds.test" /* sh */ '' set -efu command=test @@ -65,8 +63,7 @@ fi fi '' // { - parsetarget = pkgs.writeScript "init.env.parsetarget" /* sh */ '' - #! ${pkgs.dash}/bin/dash + parsetarget = pkgs.writeDash "init.env.parsetarget" '' set -efu exec ${pkgs.jq}/bin/jq \ -enr \ @@ -86,8 +83,7 @@ } ''; }; - populate = pkgs.writeScript "init.env.populate" /* sh */ '' - #! ${pkgs.dash}/bin/dash + populate = pkgs.writeDash "init.env.populate" '' set -efu ${pkgs.nix}/bin/nix-instantiate \ --eval \ @@ -102,8 +98,7 @@ "$target_user@$target_host:$target_port$target_path" \ >&2 ''; - proxy = pkgs.writeScript "init.env.proxy" /* sh */ '' - #! ${pkgs.dash}/bin/dash + proxy = pkgs.writeDash "init.env.proxy" '' set -efu q() { ${pkgs.jq}/bin/jq -nr --arg x "$*" '$x | @sh "\(.)"' @@ -124,8 +119,7 @@ ''; }; - utils.build = pkgs.writeScript "utils.build" /* sh */ '' - #! ${pkgs.dash}/bin/dash + utils.build = pkgs.writeDash "utils.build" '' set -efu expr=$1 shift @@ -140,8 +134,7 @@ ${pkgs.whatsupnix}/bin/whatsupnix ''; - utils.deploy = pkgs.writeScript "utils.deploy" /* sh */ '' - #! ${pkgs.dash}/bin/dash + utils.deploy = pkgs.writeDash "utils.deploy" '' set -efu PATH=/run/current-system/sw/bin nixos-rebuild \ switch \ @@ -153,8 +146,7 @@ ${pkgs.whatsupnix}/bin/whatsupnix ''; - hook.get-version = pkgs.writeScript "hook.get-version" /* sh */ '' - #! ${pkgs.dash}/bin/dash + hook.get-version = pkgs.writeDash "hook.get-version" '' set -efu version=git.$(${pkgs.git}/bin/git describe --always --dirty) case $version in (*-dirty) -- cgit v1.2.3 From c500d72ad855418b7d4d9d852d423a8a9440285c Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 09:43:33 +0200 Subject: shell: rename hook attribute to shell --- shell.nix | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index d6d21586..782b4fa3 100644 --- a/shell.nix +++ b/shell.nix @@ -121,32 +121,27 @@ utils.build = pkgs.writeDash "utils.build" '' set -efu - expr=$1 - shift ${pkgs.nix}/bin/nix-build \ -Q \ --no-out-link \ --show-trace \ - -E "with import ; $expr" \ + -E "with import ; $1" \ -I "$target_path" \ - "$@" \ 2>&1 | ${pkgs.whatsupnix}/bin/whatsupnix ''; utils.deploy = pkgs.writeDash "utils.deploy" '' set -efu - PATH=/run/current-system/sw/bin nixos-rebuild \ - switch \ + PATH=/run/current-system/sw/bin nixos-rebuild switch \ -Q \ --show-trace \ -I "$target_path" \ - "$@" \ 2>&1 | ${pkgs.whatsupnix}/bin/whatsupnix ''; - hook.get-version = pkgs.writeDash "hook.get-version" '' + shell.get-version = pkgs.writeDash "shell.get-version" '' set -efu version=git.$(${pkgs.git}/bin/git describe --always --dirty) case $version in (*-dirty) @@ -156,7 +151,7 @@ echo "$date.$version" ''; - hook.pkg = pkgs.runCommand "hook.pkg" {} /* sh */ '' + shell.cmdspkg = pkgs.runCommand "shell.cmdspkg" {} /* sh */ '' mkdir -p $out/bin ${lib.concatStrings (lib.mapAttrsToList (name: path: /* sh */ '' ln -s ${path} $out/bin/${name} @@ -168,7 +163,7 @@ in pkgs.stdenv.mkDerivation { shellHook = /* sh */ '' export NIX_PATH="stockholm=$PWD''${NIX_PATH+:$NIX_PATH}" export PATH=${lib.makeBinPath [ - hook.pkg + shell.cmdspkg ]} eval "$(declare -F | ${pkgs.gnused}/bin/sed s/declare/unset/)" @@ -187,7 +182,7 @@ in pkgs.stdenv.mkDerivation { } export HOSTNAME="$(${pkgs.nettools}/bin/hostname)" - export STOCKHOLM_VERSION="''${STOCKHOLM_VERSION-$(${hook.get-version})}" + export STOCKHOLM_VERSION="''${STOCKHOLM_VERSION-$(${shell.get-version})}" PS1='\[\e[38;5;162m\]\w\[\e[0m\] ' ''; -- cgit v1.2.3 From aa8fb19044cebff2d9687ab975340a7c0c96990d Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 09:44:28 +0200 Subject: shell: use writeOut to create cmdspkg --- shell.nix | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 782b4fa3..dbc2b14f 100644 --- a/shell.nix +++ b/shell.nix @@ -151,12 +151,9 @@ echo "$date.$version" ''; - shell.cmdspkg = pkgs.runCommand "shell.cmdspkg" {} /* sh */ '' - mkdir -p $out/bin - ${lib.concatStrings (lib.mapAttrsToList (name: path: /* sh */ '' - ln -s ${path} $out/bin/${name} - '') cmds)} - ''; + shell.cmdspkg = pkgs.writeOut "shell.cmdspkg" (lib.mapAttrs' (name: link: + lib.nameValuePair "/bin/${name}" { inherit link; } + ) cmds); in pkgs.stdenv.mkDerivation { name = "stockholm"; -- cgit v1.2.3 From babbed38df4c0ff90eea9daa00cea3a105fb3ab2 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 11:04:17 +0200 Subject: shell: use getopt --- shell.nix | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index dbc2b14f..541a7d68 100644 --- a/shell.nix +++ b/shell.nix @@ -3,7 +3,7 @@ inherit (nixpkgs) lib pkgs; slib = import ./lib; - # usage: deploy system=SYSTEM [target=TARGET] + # usage: deploy --system=SYSTEM [--target=TARGET] cmds.deploy = pkgs.writeDash "cmds.deploy" '' set -efu @@ -15,7 +15,7 @@ exec ${utils.deploy} ''; - # usage: test system=SYSTEM target=TARGET + # usage: test --system=SYSTEM --target=TARGET cmds.test = pkgs.writeDash "cmds.test" /* sh */ '' set -efu @@ -28,18 +28,19 @@ ''; init.args = pkgs.writeText "init.args" /* sh */ '' - fail= - for arg; do - case $arg in - system=*) system=''${arg#*=};; - target=*) target=''${arg#*=};; - *) echo "$command: bad argument: $arg" >&2; fail=1 - esac - done - if \test -n "$fail"; then - exit 1 - fi - unset fail + args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \ + -o s:t: \ + -l system:,target: \ + -- "$@") + if \test $? != 0; then exit 1; fi + eval set -- "$args" + while :; do case $1 in + -s|--system) system=$2; shift 2;; + -t|--target) target=$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 */ '' -- cgit v1.2.3 From 9ee286644d050e68292776e02357549801fd20b6 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Jun 2017 11:04:58 +0200 Subject: shell: use stockholm lib --- shell.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'shell.nix') diff --git a/shell.nix b/shell.nix index 541a7d68..3e7ba81c 100644 --- a/shell.nix +++ b/shell.nix @@ -1,7 +1,6 @@ -{ nixpkgs ? import { overlays = [(import ./krebs/5pkgs)]; } }: let - - inherit (nixpkgs) lib pkgs; - slib = import ./lib; +let + lib = import ./lib; + pkgs = import { overlays = [(import ./krebs/5pkgs)]; }; # usage: deploy --system=SYSTEM [--target=TARGET] cmds.deploy = pkgs.writeDash "cmds.deploy" '' -- cgit v1.2.3