2017-06-30 09:04:58 +00:00
|
|
|
let
|
|
|
|
lib = import ./lib;
|
|
|
|
pkgs = import <nixpkgs> { overlays = [(import ./krebs/5pkgs)]; };
|
2017-06-29 20:06:13 +00:00
|
|
|
|
2017-07-13 21:39:48 +00:00
|
|
|
# usage: deploy [--user=USER] --system=SYSTEM [--target=TARGET]
|
2017-06-30 02:56:21 +00:00
|
|
|
cmds.deploy = pkgs.writeDash "cmds.deploy" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
set -efu
|
|
|
|
|
|
|
|
command=deploy
|
|
|
|
. ${init.args}
|
|
|
|
\test -n "''${target-}" || target=$system
|
2017-07-13 21:39:48 +00:00
|
|
|
\test -n "''${user-}" || user=$LOGNAME
|
2017-06-29 20:06:13 +00:00
|
|
|
. ${init.env}
|
|
|
|
|
|
|
|
exec ${utils.deploy}
|
|
|
|
'';
|
|
|
|
|
2017-07-13 21:39:48 +00:00
|
|
|
# usage: test [--user=USER] --system=SYSTEM --target=TARGET
|
2017-06-30 02:56:21 +00:00
|
|
|
cmds.test = pkgs.writeDash "cmds.test" /* sh */ ''
|
2017-06-29 20:06:13 +00:00
|
|
|
set -efu
|
|
|
|
|
2017-07-14 01:45:05 +00:00
|
|
|
export dummy_secrets=true
|
|
|
|
|
2017-06-29 20:06:13 +00:00
|
|
|
command=test
|
|
|
|
. ${init.args}
|
2017-07-13 21:39:48 +00:00
|
|
|
\test -n "''${user-}" || user=$LOGNAME
|
2017-06-29 20:06:13 +00:00
|
|
|
. ${init.env}
|
|
|
|
|
|
|
|
exec ${utils.build} config.system.build.toplevel
|
|
|
|
'';
|
|
|
|
|
|
|
|
init.args = pkgs.writeText "init.args" /* sh */ ''
|
2017-06-30 09:04:17 +00:00
|
|
|
args=$(${pkgs.utillinux}/bin/getopt -n "$command" -s sh \
|
2017-07-13 21:39:48 +00:00
|
|
|
-o s:t:u: \
|
|
|
|
-l system:,target:,user: \
|
2017-06-30 09:04:17 +00:00
|
|
|
-- "$@")
|
|
|
|
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;;
|
2017-07-13 21:39:48 +00:00
|
|
|
-u|--user) user=$2; shift 2;;
|
2017-06-30 09:04:17 +00:00
|
|
|
--) shift; break;;
|
|
|
|
esac; done
|
|
|
|
for arg; do echo "$command: bad argument: $arg" >&2; done
|
|
|
|
if \test $# != 0; then exit 2; fi
|
2017-06-29 20:06:13 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
init.env = pkgs.writeText "init.env" /* sh */ ''
|
2017-07-13 21:39:48 +00:00
|
|
|
config=''${config-$user/1systems/$system/config.nix}
|
|
|
|
source=''${source-$user/1systems/$system/source.nix}
|
2017-06-29 20:06:13 +00:00
|
|
|
|
|
|
|
export config
|
2017-07-06 19:47:47 +00:00
|
|
|
export source
|
2017-06-29 20:06:13 +00:00
|
|
|
export system
|
|
|
|
export target
|
2017-07-13 21:39:48 +00:00
|
|
|
export user
|
2017-06-29 20:06:13 +00:00
|
|
|
|
|
|
|
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)"
|
|
|
|
|
2017-06-30 02:27:02 +00:00
|
|
|
if \test "''${using_proxy-}" != true; then
|
|
|
|
${init.env.populate}
|
|
|
|
if \test "$target_local" != true; then
|
|
|
|
exec ${init.env.proxy} "$command" "$@"
|
|
|
|
fi
|
2017-06-29 20:06:13 +00:00
|
|
|
fi
|
|
|
|
'' // {
|
2017-06-30 02:56:21 +00:00
|
|
|
parsetarget = pkgs.writeDash "init.env.parsetarget" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2017-06-30 02:56:21 +00:00
|
|
|
populate = pkgs.writeDash "init.env.populate" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
set -efu
|
2017-07-08 12:58:09 +00:00
|
|
|
_source=$(${pkgs.nix}/bin/nix-instantiate \
|
2017-06-29 20:06:13 +00:00
|
|
|
--eval \
|
|
|
|
--json \
|
|
|
|
--readonly-mode \
|
|
|
|
--show-trace \
|
|
|
|
--strict \
|
|
|
|
-I nixos-config="$config" \
|
2017-07-06 19:47:47 +00:00
|
|
|
"$source")
|
2017-07-08 12:58:09 +00:00
|
|
|
echo $_source |
|
2017-06-30 02:40:45 +00:00
|
|
|
${pkgs.populate}/bin/populate \
|
2017-06-30 02:27:25 +00:00
|
|
|
"$target_user@$target_host:$target_port$target_path" \
|
|
|
|
>&2
|
2017-07-08 12:58:09 +00:00
|
|
|
unset _source
|
2017-06-29 20:06:13 +00:00
|
|
|
'';
|
2017-06-30 02:56:21 +00:00
|
|
|
proxy = pkgs.writeDash "init.env.proxy" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
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 \
|
2017-07-08 13:00:30 +00:00
|
|
|
--run $(q \
|
2017-06-29 20:06:13 +00:00
|
|
|
config=$config \
|
|
|
|
system=$system \
|
|
|
|
target=$target \
|
2017-06-30 02:27:02 +00:00
|
|
|
using_proxy=true \
|
2017-06-29 20:06:13 +00:00
|
|
|
"$*"
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-06-30 02:56:21 +00:00
|
|
|
utils.build = pkgs.writeDash "utils.build" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
set -efu
|
|
|
|
${pkgs.nix}/bin/nix-build \
|
|
|
|
-Q \
|
|
|
|
--no-out-link \
|
|
|
|
--show-trace \
|
2017-06-30 07:43:33 +00:00
|
|
|
-E "with import <stockholm>; $1" \
|
2017-06-29 20:06:13 +00:00
|
|
|
-I "$target_path" \
|
|
|
|
2>&1 |
|
2017-06-30 02:53:34 +00:00
|
|
|
${pkgs.whatsupnix}/bin/whatsupnix
|
2017-06-29 20:06:13 +00:00
|
|
|
'';
|
|
|
|
|
2017-06-30 02:56:21 +00:00
|
|
|
utils.deploy = pkgs.writeDash "utils.deploy" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
set -efu
|
2017-06-30 07:43:33 +00:00
|
|
|
PATH=/run/current-system/sw/bin nixos-rebuild switch \
|
2017-06-29 20:06:13 +00:00
|
|
|
-Q \
|
|
|
|
--show-trace \
|
|
|
|
-I "$target_path" \
|
|
|
|
2>&1 |
|
2017-06-30 02:53:34 +00:00
|
|
|
${pkgs.whatsupnix}/bin/whatsupnix
|
2017-06-29 20:06:13 +00:00
|
|
|
'';
|
|
|
|
|
2017-06-30 07:43:33 +00:00
|
|
|
shell.get-version = pkgs.writeDash "shell.get-version" ''
|
2017-06-29 20:06:13 +00:00
|
|
|
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"
|
|
|
|
'';
|
|
|
|
|
2017-06-30 07:44:28 +00:00
|
|
|
shell.cmdspkg = pkgs.writeOut "shell.cmdspkg" (lib.mapAttrs' (name: link:
|
|
|
|
lib.nameValuePair "/bin/${name}" { inherit link; }
|
|
|
|
) cmds);
|
2017-06-29 20:06:13 +00:00
|
|
|
|
|
|
|
in pkgs.stdenv.mkDerivation {
|
|
|
|
name = "stockholm";
|
2017-06-30 01:16:24 +00:00
|
|
|
shellHook = /* sh */ ''
|
2017-06-29 20:06:13 +00:00
|
|
|
export NIX_PATH="stockholm=$PWD''${NIX_PATH+:$NIX_PATH}"
|
|
|
|
export PATH=${lib.makeBinPath [
|
2017-06-30 07:43:33 +00:00
|
|
|
shell.cmdspkg
|
2017-06-29 20:06:13 +00:00
|
|
|
]}
|
|
|
|
|
|
|
|
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)"
|
2017-06-30 07:43:33 +00:00
|
|
|
export STOCKHOLM_VERSION="''${STOCKHOLM_VERSION-$(${shell.get-version})}"
|
2017-06-29 20:06:13 +00:00
|
|
|
|
|
|
|
PS1='\[\e[38;5;162m\]\w\[\e[0m\] '
|
|
|
|
'';
|
|
|
|
}
|