2014-10-17 15:01:12 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2023-04-30 12:58:29 +00:00
|
|
|
let
|
|
|
|
cfg = config.services.cloud-init;
|
|
|
|
path = with pkgs; [
|
|
|
|
cloud-init
|
|
|
|
iproute2
|
|
|
|
nettools
|
|
|
|
openssh
|
|
|
|
shadow
|
|
|
|
util-linux
|
|
|
|
busybox
|
|
|
|
]
|
|
|
|
++ optional cfg.btrfs.enable btrfs-progs
|
|
|
|
++ optional cfg.ext4.enable e2fsprogs
|
2023-07-04 17:05:41 +00:00
|
|
|
++ optional cfg.xfs.enable xfsprogs
|
2023-04-30 12:58:29 +00:00
|
|
|
;
|
2023-04-30 12:05:01 +00:00
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
|
|
|
cfgfile = settingsFormat.generate "cloud.cfg" cfg.settings;
|
2014-10-17 15:01:12 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.cloud-init = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-04-30 12:59:16 +00:00
|
|
|
description = mdDoc ''
|
2014-10-17 15:01:12 +00:00
|
|
|
Enable the cloud-init service. This services reads
|
|
|
|
configuration metadata in a cloud environment and configures
|
|
|
|
the machine according to this metadata.
|
|
|
|
|
|
|
|
This configuration is not completely compatible with the
|
|
|
|
NixOS way of doing configuration, as configuration done by
|
2022-12-18 00:31:14 +00:00
|
|
|
cloud-init might be overridden by a subsequent nixos-rebuild
|
2014-10-17 15:01:12 +00:00
|
|
|
call. However, some parts of cloud-init fall outside of
|
|
|
|
NixOS's responsibility, like filesystem resizing and ssh
|
|
|
|
public key provisioning, and cloud-init is useful for that
|
|
|
|
parts. Thus, be wary that using cloud-init in NixOS might
|
|
|
|
come as some cost.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-11-13 10:28:40 +00:00
|
|
|
btrfs.enable = mkOption {
|
2018-11-10 16:07:48 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-04-30 12:59:16 +00:00
|
|
|
description = mdDoc ''
|
2018-11-10 16:07:48 +00:00
|
|
|
Allow the cloud-init service to operate `btrfs` filesystem.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-11-13 10:28:40 +00:00
|
|
|
ext4.enable = mkOption {
|
2018-11-10 16:07:48 +00:00
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-04-30 12:59:16 +00:00
|
|
|
description = mdDoc ''
|
2018-11-10 16:07:48 +00:00
|
|
|
Allow the cloud-init service to operate `ext4` filesystem.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-07-04 17:05:41 +00:00
|
|
|
xfs.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = mdDoc ''
|
|
|
|
Allow the cloud-init service to operate `xfs` filesystem.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-01-28 13:46:29 +00:00
|
|
|
network.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-04-30 12:59:16 +00:00
|
|
|
description = mdDoc ''
|
2022-01-28 13:46:29 +00:00
|
|
|
Allow the cloud-init service to configure network interfaces
|
|
|
|
through systemd-networkd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-04-30 12:05:01 +00:00
|
|
|
settings = mkOption {
|
|
|
|
description = mdDoc ''
|
|
|
|
Structured cloud-init configuration.
|
|
|
|
'';
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
};
|
|
|
|
default = { };
|
|
|
|
};
|
|
|
|
|
2017-02-20 16:24:57 +00:00
|
|
|
config = mkOption {
|
|
|
|
type = types.str;
|
2023-04-30 12:05:01 +00:00
|
|
|
default = "";
|
|
|
|
description = mdDoc ''
|
|
|
|
raw cloud-init configuration.
|
|
|
|
|
|
|
|
Takes precedence over the `settings` option if set.
|
2023-04-30 12:58:29 +00:00
|
|
|
'';
|
2017-02-20 16:24:57 +00:00
|
|
|
};
|
|
|
|
|
2014-10-17 15:01:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2023-05-14 20:35:06 +00:00
|
|
|
config = mkIf cfg.enable {
|
2023-04-30 12:05:01 +00:00
|
|
|
services.cloud-init.settings = {
|
|
|
|
system_info = mkDefault {
|
|
|
|
distro = "nixos";
|
|
|
|
network = {
|
|
|
|
renderers = [ "networkd" ];
|
|
|
|
};
|
|
|
|
};
|
2014-10-17 15:01:12 +00:00
|
|
|
|
2023-04-30 12:05:01 +00:00
|
|
|
users = mkDefault [ "root" ];
|
|
|
|
disable_root = mkDefault false;
|
|
|
|
preserve_hostname = mkDefault false;
|
|
|
|
|
|
|
|
cloud_init_modules = mkDefault [
|
|
|
|
"migrator"
|
|
|
|
"seed_random"
|
|
|
|
"bootcmd"
|
|
|
|
"write-files"
|
|
|
|
"growpart"
|
|
|
|
"resizefs"
|
|
|
|
"update_hostname"
|
|
|
|
"resolv_conf"
|
|
|
|
"ca-certs"
|
|
|
|
"rsyslog"
|
|
|
|
"users-groups"
|
|
|
|
];
|
|
|
|
|
|
|
|
cloud_config_modules = mkDefault [
|
|
|
|
"disk_setup"
|
|
|
|
"mounts"
|
|
|
|
"ssh-import-id"
|
|
|
|
"set-passwords"
|
|
|
|
"timezone"
|
|
|
|
"disable-ec2-metadata"
|
|
|
|
"runcmd"
|
|
|
|
"ssh"
|
|
|
|
];
|
|
|
|
|
|
|
|
cloud_final_modules = mkDefault [
|
|
|
|
"rightscale_userdata"
|
|
|
|
"scripts-vendor"
|
|
|
|
"scripts-per-once"
|
|
|
|
"scripts-per-boot"
|
|
|
|
"scripts-per-instance"
|
|
|
|
"scripts-user"
|
|
|
|
"ssh-authkey-fingerprints"
|
|
|
|
"keys-to-console"
|
|
|
|
"phone-home"
|
|
|
|
"final-message"
|
|
|
|
"power-state-change"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc."cloud/cloud.cfg" =
|
|
|
|
if cfg.config == "" then
|
|
|
|
{ source = cfgfile; }
|
|
|
|
else
|
|
|
|
{ text = cfg.config; }
|
|
|
|
;
|
2017-02-20 16:24:57 +00:00
|
|
|
|
2022-01-28 13:46:29 +00:00
|
|
|
systemd.network.enable = cfg.network.enable;
|
|
|
|
|
2023-04-30 12:58:29 +00:00
|
|
|
systemd.services.cloud-init-local = {
|
|
|
|
description = "Initial cloud-init job (pre-networking)";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
before = [ "systemd-networkd.service" ];
|
|
|
|
path = path;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.cloud-init}/bin/cloud-init init --local";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
TimeoutSec = "infinity";
|
|
|
|
StandardOutput = "journal+console";
|
2014-10-17 15:01:12 +00:00
|
|
|
};
|
2023-04-30 12:58:29 +00:00
|
|
|
};
|
2014-10-17 15:01:12 +00:00
|
|
|
|
2023-04-30 12:58:29 +00:00
|
|
|
systemd.services.cloud-init = {
|
|
|
|
description = "Initial cloud-init job (metadata service crawler)";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [
|
|
|
|
"network-online.target"
|
|
|
|
"cloud-init-local.service"
|
|
|
|
"sshd.service"
|
|
|
|
"sshd-keygen.service"
|
|
|
|
];
|
|
|
|
after = [ "network-online.target" "cloud-init-local.service" ];
|
|
|
|
before = [ "sshd.service" "sshd-keygen.service" ];
|
|
|
|
requires = [ "network.target" ];
|
|
|
|
path = path;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.cloud-init}/bin/cloud-init init";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
TimeoutSec = "infinity";
|
|
|
|
StandardOutput = "journal+console";
|
2014-10-17 15:01:12 +00:00
|
|
|
};
|
2023-04-30 12:58:29 +00:00
|
|
|
};
|
2014-10-17 15:01:12 +00:00
|
|
|
|
2023-04-30 12:58:29 +00:00
|
|
|
systemd.services.cloud-config = {
|
|
|
|
description = "Apply the settings specified in cloud-config";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" "syslog.target" "cloud-config.target" ];
|
|
|
|
|
|
|
|
path = path;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.cloud-init}/bin/cloud-init modules --mode=config";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
TimeoutSec = "infinity";
|
|
|
|
StandardOutput = "journal+console";
|
2014-10-17 15:01:12 +00:00
|
|
|
};
|
2023-04-30 12:58:29 +00:00
|
|
|
};
|
2014-10-17 15:01:12 +00:00
|
|
|
|
2023-04-30 12:58:29 +00:00
|
|
|
systemd.services.cloud-final = {
|
|
|
|
description = "Execute cloud user/final scripts";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" "syslog.target" "cloud-config.service" "rc-local.service" ];
|
|
|
|
requires = [ "cloud-config.target" ];
|
|
|
|
path = path;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.cloud-init}/bin/cloud-init modules --mode=final";
|
|
|
|
RemainAfterExit = "yes";
|
|
|
|
TimeoutSec = "infinity";
|
|
|
|
StandardOutput = "journal+console";
|
2014-10-17 15:01:12 +00:00
|
|
|
};
|
2023-04-30 12:58:29 +00:00
|
|
|
};
|
2014-10-17 15:01:12 +00:00
|
|
|
|
2023-04-30 12:58:29 +00:00
|
|
|
systemd.targets.cloud-config = {
|
|
|
|
description = "Cloud-config availability";
|
|
|
|
requires = [ "cloud-init-local.service" "cloud-init.service" ];
|
|
|
|
};
|
2023-05-14 20:35:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
meta.maintainers = [ maintainers.zimbatm ];
|
2014-10-17 15:01:12 +00:00
|
|
|
}
|