2021-02-07 22:00:00 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
2015-04-10 06:06:52 +02:00
|
|
|
|
|
|
|
let
|
2021-02-07 22:00:00 +01:00
|
|
|
cfg = config.virtualisation.amazon-init;
|
|
|
|
|
2017-02-15 23:32:45 +01:00
|
|
|
script = ''
|
2018-03-01 20:38:53 +01:00
|
|
|
#!${pkgs.runtimeShell} -eu
|
2015-04-10 06:06:52 +02:00
|
|
|
|
2016-02-04 15:15:19 +01:00
|
|
|
echo "attempting to fetch configuration from EC2 user data..."
|
2015-04-10 06:06:52 +02:00
|
|
|
|
2017-02-15 23:32:45 +01:00
|
|
|
export HOME=/root
|
2020-04-14 20:39:48 +02:00
|
|
|
export PATH=${pkgs.lib.makeBinPath [ config.nix.package pkgs.systemd pkgs.gnugrep pkgs.git pkgs.gnutar pkgs.gzip pkgs.gnused pkgs.xz config.system.build.nixos-rebuild]}:$PATH
|
2019-12-27 20:32:12 +01:00
|
|
|
export NIX_PATH=nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
|
2015-04-10 06:06:52 +02:00
|
|
|
|
2016-02-04 15:42:49 +01:00
|
|
|
userData=/etc/ec2-metadata/user-data
|
2015-04-10 06:06:52 +02:00
|
|
|
|
2016-02-04 15:42:49 +01:00
|
|
|
if [ -s "$userData" ]; then
|
2015-04-10 06:06:52 +02:00
|
|
|
# If the user-data looks like it could be a nix expression,
|
|
|
|
# copy it over. Also, look for a magic three-hash comment and set
|
|
|
|
# that as the channel.
|
|
|
|
if sed '/^\(#\|SSH_HOST_.*\)/d' < "$userData" | grep -q '\S'; then
|
|
|
|
channels="$(grep '^###' "$userData" | sed 's|###\s*||')"
|
2019-12-27 20:32:12 +01:00
|
|
|
while IFS= read -r channel; do
|
2015-04-10 06:06:52 +02:00
|
|
|
echo "writing channel: $channel"
|
2019-12-27 20:32:12 +01:00
|
|
|
done < <(printf "%s\n" "$channels")
|
2015-04-10 06:06:52 +02:00
|
|
|
|
|
|
|
if [[ -n "$channels" ]]; then
|
|
|
|
printf "%s" "$channels" > /root/.nix-channels
|
|
|
|
nix-channel --update
|
|
|
|
fi
|
|
|
|
|
2016-02-04 15:15:19 +01:00
|
|
|
echo "setting configuration from EC2 user data"
|
2015-04-10 06:06:52 +02:00
|
|
|
cp "$userData" /etc/nixos/configuration.nix
|
|
|
|
else
|
2016-02-04 15:15:19 +01:00
|
|
|
echo "user data does not appear to be a Nix expression; ignoring"
|
|
|
|
exit
|
2015-04-10 06:06:52 +02:00
|
|
|
fi
|
|
|
|
else
|
2016-02-04 15:42:49 +01:00
|
|
|
echo "no user data is available"
|
2016-02-04 15:15:19 +01:00
|
|
|
exit
|
2015-04-10 06:06:52 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
nixos-rebuild switch
|
|
|
|
'';
|
|
|
|
in {
|
2017-02-15 23:32:45 +01:00
|
|
|
|
2021-02-07 22:00:00 +01:00
|
|
|
options.virtualisation.amazon-init = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
Enable or disable the amazon-init service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.amazon-init = {
|
|
|
|
inherit script;
|
|
|
|
description = "Reconfigure the system from EC2 userdata on startup";
|
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "multi-user.target" ];
|
|
|
|
requires = [ "network-online.target" ];
|
2019-12-29 13:00:35 +01:00
|
|
|
|
2021-02-07 22:00:00 +01:00
|
|
|
restartIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
2017-02-15 23:32:45 +01:00
|
|
|
|
2021-02-07 22:00:00 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
2017-02-15 23:32:45 +01:00
|
|
|
};
|
|
|
|
};
|
2015-04-10 06:06:52 +02:00
|
|
|
}
|