1 { config, pkgs, lib, ... }:
4 cfg = config.virtualisation.digitalOcean;
5 defaultConfigFile = pkgs.writeText "digitalocean-configuration.nix" ''
6 { modulesPath, lib, ... }:
8 imports = lib.optional (builtins.pathExists ./do-userdata.nix) ./do-userdata.nix ++ [
9 (modulesPath + "/virtualisation/digital-ocean-config.nix")
14 options.virtualisation.digitalOcean.rebuildFromUserData = mkOption {
18 description = "Whether to reconfigure the system from Digital Ocean user data";
20 options.virtualisation.digitalOcean.defaultConfigFile = mkOption {
22 default = defaultConfigFile;
23 defaultText = literalMD ''
24 The default configuration imports user-data if applicable and
25 `(modulesPath + "/virtualisation/digital-ocean-config.nix")`.
28 A path to a configuration file which will be placed at
29 `/etc/nixos/configuration.nix` and be used when switching to
35 systemd.services.digitalocean-init = mkIf cfg.rebuildFromUserData {
36 description = "Reconfigure the system from Digital Ocean userdata on startup";
37 wantedBy = [ "network-online.target" ];
39 ConditionPathExists = "!/etc/nixos/do-userdata.nix";
40 After = [ "digitalocean-metadata.service" "network-online.target" ];
41 Requires = [ "digitalocean-metadata.service" ];
42 X-StopOnRemoval = false;
46 RemainAfterExit = true;
48 restartIfChanged = false;
49 path = [ pkgs.jq pkgs.gnused pkgs.gnugrep config.systemd.package config.nix.package config.system.build.nixos-rebuild ];
52 NIX_PATH = concatStringsSep ":" [
53 "/nix/var/nix/profiles/per-user/root/channels/nixos"
54 "nixos-config=/etc/nixos/configuration.nix"
55 "/nix/var/nix/profiles/per-user/root/channels"
60 echo "attempting to fetch configuration from Digital Ocean user data..."
62 if jq -er '.user_data' /run/do-metadata/v1.json > $userData; then
63 # If the user-data looks like it could be a nix expression,
64 # copy it over. Also, look for a magic three-hash comment and set
65 # that as the channel.
66 if nix-instantiate --parse $userData > /dev/null; then
67 channels="$(grep '^###' "$userData" | sed 's|###\s*||')"
68 printf "%s" "$channels" | while read channel; do
69 echo "writing channel: $channel"
72 if [[ -n "$channels" ]]; then
73 printf "%s" "$channels" > /root/.nix-channels
77 echo "setting configuration from Digital Ocean user data"
78 cp "$userData" /etc/nixos/do-userdata.nix
79 if [[ ! -e /etc/nixos/configuration.nix ]]; then
80 install -m0644 ${cfg.defaultConfigFile} /etc/nixos/configuration.nix
83 echo "user data does not appear to be a Nix expression; ignoring"
89 echo "no user data is available"
94 meta.maintainers = with maintainers; [ arianvp eamsden ];