9 cfg = config.virtualisation.digitalOcean;
10 defaultConfigFile = pkgs.writeText "digitalocean-configuration.nix" ''
11 { modulesPath, lib, ... }:
13 imports = lib.optional (builtins.pathExists ./do-userdata.nix) ./do-userdata.nix ++ [
14 (modulesPath + "/virtualisation/digital-ocean-config.nix")
20 options.virtualisation.digitalOcean.rebuildFromUserData = mkOption {
24 description = "Whether to reconfigure the system from Digital Ocean user data";
26 options.virtualisation.digitalOcean.defaultConfigFile = mkOption {
28 default = defaultConfigFile;
29 defaultText = literalMD ''
30 The default configuration imports user-data if applicable and
31 `(modulesPath + "/virtualisation/digital-ocean-config.nix")`.
34 A path to a configuration file which will be placed at
35 `/etc/nixos/configuration.nix` and be used when switching to
41 systemd.services.digitalocean-init = mkIf cfg.rebuildFromUserData {
42 description = "Reconfigure the system from Digital Ocean userdata on startup";
43 wantedBy = [ "network-online.target" ];
45 ConditionPathExists = "!/etc/nixos/do-userdata.nix";
47 "digitalocean-metadata.service"
48 "network-online.target"
50 Requires = [ "digitalocean-metadata.service" ];
51 X-StopOnRemoval = false;
55 RemainAfterExit = true;
57 restartIfChanged = false;
62 config.systemd.package
64 config.system.build.nixos-rebuild
68 NIX_PATH = concatStringsSep ":" [
69 "/nix/var/nix/profiles/per-user/root/channels/nixos"
70 "nixos-config=/etc/nixos/configuration.nix"
71 "/nix/var/nix/profiles/per-user/root/channels"
76 echo "attempting to fetch configuration from Digital Ocean user data..."
78 if jq -er '.user_data' /run/do-metadata/v1.json > $userData; then
79 # If the user-data looks like it could be a nix expression,
80 # copy it over. Also, look for a magic three-hash comment and set
81 # that as the channel.
82 if nix-instantiate --parse $userData > /dev/null; then
83 channels="$(grep '^###' "$userData" | sed 's|###\s*||')"
84 printf "%s" "$channels" | while read channel; do
85 echo "writing channel: $channel"
88 if [[ -n "$channels" ]]; then
89 printf "%s" "$channels" > /root/.nix-channels
93 echo "setting configuration from Digital Ocean user data"
94 cp "$userData" /etc/nixos/do-userdata.nix
95 if [[ ! -e /etc/nixos/configuration.nix ]]; then
96 install -m0644 ${cfg.defaultConfigFile} /etc/nixos/configuration.nix
99 echo "user data does not appear to be a Nix expression; ignoring"
105 echo "no user data is available"
110 meta.maintainers = with maintainers; [