···
1
+
{ config, lib, pkgs, ... }:
5
+
let cfg = config.services.cloud-init;
6
+
path = with pkgs; [ cloud-init nettools utillinux e2fsprogs shadow dmidecode openssh ];
7
+
configFile = pkgs.writeText "cloud-init.cfg" ''
12
+
preserve_hostname: false
28
+
cloud_config_modules:
35
+
- disable-ec2-metadata
39
+
cloud_final_modules:
40
+
- rightscale_userdata
44
+
- scripts-per-instance
46
+
- ssh-authkey-fingerprints
50
+
- power-state-change
56
+
services.cloud-init = {
62
+
Enable the cloud-init service. This services reads
63
+
configuration metadata in a cloud environment and configures
64
+
the machine according to this metadata.
66
+
This configuration is not completely compatible with the
67
+
NixOS way of doing configuration, as configuration done by
68
+
cloud-init might be overriden by a subsequent nixos-rebuild
69
+
call. However, some parts of cloud-init fall outside of
70
+
NixOS's responsibility, like filesystem resizing and ssh
71
+
public key provisioning, and cloud-init is useful for that
72
+
parts. Thus, be wary that using cloud-init in NixOS might
81
+
config = mkIf cfg.enable {
83
+
systemd.services.cloud-init-local =
84
+
{ description = "Initial cloud-init job (pre-networking)";
85
+
wantedBy = [ "multi-user.target" ];
86
+
wants = [ "local-fs.target" ];
87
+
after = [ "local-fs.target" ];
91
+
ExecStart = "${pkgs.cloud-init}/bin/cloud-init -f ${configFile} init --local";
92
+
RemainAfterExit = "yes";
94
+
StandardOutput = "journal+console";
98
+
systemd.services.cloud-init =
99
+
{ description = "Initial cloud-init job (metadata service crawler)";
100
+
wantedBy = [ "multi-user.target" ];
101
+
wants = [ "local-fs.target" "cloud-init-local.service" "sshd.service" "sshd-keygen.service" ];
102
+
after = [ "local-fs.target" "network.target" "cloud-init-local.service" ];
103
+
before = [ "sshd.service" "sshd-keygen.service" ];
104
+
requires = [ "network.target "];
107
+
{ Type = "oneshot";
108
+
ExecStart = "${pkgs.cloud-init}/bin/cloud-init -f ${configFile} init";
109
+
RemainAfterExit = "yes";
111
+
StandardOutput = "journal+console";
115
+
systemd.services.cloud-config =
116
+
{ description = "Apply the settings specified in cloud-config";
117
+
wantedBy = [ "multi-user.target" ];
118
+
wants = [ "network.target" ];
119
+
after = [ "network.target" "syslog.target" "cloud-config.target" ];
123
+
{ Type = "oneshot";
124
+
ExecStart = "${pkgs.cloud-init}/bin/cloud-init -f ${configFile} modules --mode=config";
125
+
RemainAfterExit = "yes";
127
+
StandardOutput = "journal+console";
131
+
systemd.services.cloud-final =
132
+
{ description = "Execute cloud user/final scripts";
133
+
wantedBy = [ "multi-user.target" ];
134
+
wants = [ "network.target" ];
135
+
after = [ "network.target" "syslog.target" "cloud-config.service" "rc-local.service" ];
136
+
requires = [ "cloud-config.target" ];
139
+
{ Type = "oneshot";
140
+
ExecStart = "${pkgs.cloud-init}/bin/cloud-init -f ${configFile} modules --mode=final";
141
+
RemainAfterExit = "yes";
143
+
StandardOutput = "journal+console";
147
+
systemd.targets.cloud-config =
148
+
{ description = "Cloud-config availability";
149
+
requires = [ "cloud-init-local.service" "cloud-init.service" ];