summary refs log tree commit diff
diff options
context:
space:
mode:
authorDan Peebles <pumpkin@me.com>2017-02-15 22:32:45 +0000
committerDan Peebles <pumpkin@me.com>2017-02-16 16:03:58 +0000
commitb172684c17aa1eea76a996ab888cef13ea7a16af (patch)
tree1af47f69ae99afa5df59d09d02e72c17a5b9a761
parent7943dc897895a844b5c7bbf02e4b216cf6a1fdf2 (diff)
downloadnixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.tar
nixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.tar.gz
nixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.tar.bz2
nixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.tar.lz
nixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.tar.xz
nixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.tar.zst
nixpkgs-b172684c17aa1eea76a996ab888cef13ea7a16af.zip
amazon-init NixOS module: fix (I think) race condition with network
The initialization code is now a systemd service that explicitly
waits for network-online, so the occasional failure I was seeing
because the `nixos-rebuild` couldn't get anything from the binary
cache should stop. I hope!
-rw-r--r--nixos/modules/virtualisation/amazon-init.nix29
1 files changed, 21 insertions, 8 deletions
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;
+    };
+  };
 }
+