-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.nix
159 lines (136 loc) · 4.35 KB
/
default.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{ kernelLatest ? true, diskSize ? 2048 }:
with import <nixpkgs> {};
with lib;
let
cloudConfigFile = pkgs.writeText "cloud-config"
(''
{ config, lib, pkgs, ... }:
with lib;
let
waitNetworkUp = pkgs.writeScriptBin "wait-network-up" '''
#!''${pkgs.bash}/bin/bash
PATH=''${makeBinPath [ pkgs.nettools ]}:$PATH
while true; do
echo "Checking for IPv4 default route"
netstat -rn | grep -q '^0\.0\.0\.0' && break
sleep 1
done
echo "Default route present, marking network as up"
''';
in
{
imports = [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix> ];
config = {
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
fsType = "ext4";
autoResize = true;
};
boot.growPartition = true;
boot.kernelParams = [ "console=tty1" ];
boot.loader.grub.device = "/dev/vda";
boot.loader.timeout = 0;
'' + (if kernelLatest == true then
" boot.kernelPackages = pkgs.linuxPackages_latest;\n\n"
else
"") +
''
services.openssh = {
enable = true;
permitRootLogin = "prohibit-password";
passwordAuthentication = mkDefault false;
authorizedKeysFiles = mkOverride 1 [ ".ssh/authorized_keys" ];
};
# Cloud-init configuration.
services.cloud-init.enable = true;
services.cloud-init.config =
'''
system_info:
distro: nixos
default_user:
name: nixos
users:
- default
disable_root: true
preserve_hostname: false
datasource_list: [ 'OpenStack', 'Ec2' ]
cloud_init_modules:
- migrator
- seed_random
- bootcmd
- write-files
- growpart
- resizefs
- update_etc_hosts
- ca-certs
- rsyslog
- users-groups
cloud_config_modules:
- disk_setup
- mounts
- ssh-import-id
- set-passwords
- timezone
- disable-ec2-metadata
- runcmd
- ssh
cloud_final_modules:
- 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
''';
users.extraUsers.nixos = {
isNormalUser = true;
};
security.sudo.extraConfig = '''
nixos ALL=(ALL:ALL) NOPASSWD: ALL
''';
systemd.services.wait-network-up =
{ description = "Check network target";
requiredBy = [ "network-online.target" ];
wants = [ "network.target" ];
after = [ "network.target" ];
serviceConfig =
{ Type = "oneshot";
ExecStart = "''${waitNetworkUp}/bin/wait-network-up";
RemainAfterExit = "yes";
TimeoutSec = "0";
StandardOutput = "journal+console";
};
};
};
}
'');
configFile = pkgs.writeText "configuration.nix"
(''
{ config, lib, pkgs, ... }:
{
imports = [ ./cloud-config.nix ];
}
'');
cloudImageModule =
{ config, lib, pkgs, ... }:
with lib;
{
system.build.cloudImage = import <nixpkgs/nixos/lib/make-disk-image.nix> {
inherit lib config diskSize configFile;
contents = [ { source = cloudConfigFile; target = "/etc/nixos/cloud-config.nix"; } ];
pkgs = import <nixpkgs> { inherit (pkgs) system; }; # ensure we use the regular qemu-kvm package
format = "raw";
};
};
eval = import <nixpkgs/nixos/lib/eval-config.nix> {
modules = [ (import cloudConfigFile) cloudImageModule ];
};
in
{
inherit (eval.config.system.build) cloudImage;
}