diff --git a/nixos/modules/virtualisation/amazon-init.nix b/nixos/modules/virtualisation/amazon-init.nix index c9356c9b4ea..5797d9db436 100644 --- a/nixos/modules/virtualisation/amazon-init.nix +++ b/nixos/modules/virtualisation/amazon-init.nix @@ -1,20 +1,18 @@ -{ config, pkgs, modulesPath, ... }: - -# This attempts to pull a nix expression from this EC2 instance's user-data. +{ config, pkgs, ... }: let - bootScript = pkgs.writeScript "bootscript.sh" '' + script = '' #!${pkgs.stdenv.shell} -eu echo "attempting to fetch configuration from EC2 user data..." + export HOME=/root export PATH=${pkgs.lib.makeBinPath [ config.nix.package pkgs.systemd pkgs.gnugrep pkgs.gnused config.system.build.nixos-rebuild]}:$PATH export NIX_PATH=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels userData=/etc/ec2-metadata/user-data if [ -s "$userData" ]; then - # 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. @@ -43,7 +41,22 @@ let nixos-rebuild switch ''; in { - boot.postBootCommands = '' - ${bootScript} & - ''; + systemd.services.amazon-init = { + inherit script; + description = "Reconfigure the system from EC2 userdata on startup"; + + wantedBy = [ "sshd.service" ]; + before = [ "sshd.service" ]; + after = [ "network-online.target" ]; + requires = [ "network-online.target" ]; + + restartIfChanged = false; + unitConfig.X-StopOnRemoval = false; + + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + }; } +