forked from mitchellh/nixos-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
122 lines (109 loc) · 4.27 KB
/
Makefile
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
# Connectivity info for Linux VM
NIXADDR ?= unset
NIXPORT ?= 22
NIXUSER ?= mitchellh
# Get the path to this Makefile and directory
MAKEFILE_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
# The name of the nixosConfiguration in the flake
NIXNAME ?= vm-intel
# SSH options that are used. These aren't meant to be overridden but are
# reused a lot so we just store them up here.
SSH_OPTIONS=-o PubkeyAuthentication=no -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
# We need to do some OS switching below.
UNAME := $(shell uname)
switch:
ifeq ($(UNAME), Darwin)
nix build ".#darwinConfigurations.${NIXNAME}.system"
./result/sw/bin/darwin-rebuild switch --flake "$$(pwd)#${NIXNAME}"
else
sudo NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nixos-rebuild switch --flake ".#${NIXNAME}"
endif
test:
ifeq ($(UNAME), Darwin)
nix build ".#darwinConfigurations.${NIXNAME}.system"
./result/sw/bin/darwin-rebuild test --flake "$$(pwd)#${NIXNAME}"
else
sudo NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nixos-rebuild test --flake ".#$(NIXNAME)"
endif
# This builds the given NixOS configuration and pushes the results to the
# cache. This does not alter the current running system. This requires
# cachix authentication to be configured out of band.
cache:
nix build '.#nixosConfigurations.$(NIXNAME).config.system.build.toplevel' --json \
| jq -r '.[].outputs | to_entries[].value' \
| cachix push mitchellh-nixos-config
# bootstrap a brand new VM. The VM should have NixOS ISO on the CD drive
# and just set the password of the root user to "root". This will install
# NixOS. After installing NixOS, you must reboot and set the root password
# for the next step.
#
# NOTE(mitchellh): I'm sure there is a way to do this and bootstrap all
# in one step but when I tried to merge them I got errors. One day.
vm/bootstrap0:
ssh $(SSH_OPTIONS) -p$(NIXPORT) root@$(NIXADDR) " \
parted /dev/sda -- mklabel gpt; \
parted /dev/sda -- mkpart primary 512MB -8GB; \
parted /dev/sda -- mkpart primary linux-swap -8GB 100\%; \
parted /dev/sda -- mkpart ESP fat32 1MB 512MB; \
parted /dev/sda -- set 3 esp on; \
sleep 1; \
mkfs.ext4 -L nixos /dev/sda1; \
mkswap -L swap /dev/sda2; \
mkfs.fat -F 32 -n boot /dev/sda3; \
sleep 1; \
mount /dev/disk/by-label/nixos /mnt; \
mkdir -p /mnt/boot; \
mount /dev/disk/by-label/boot /mnt/boot; \
nixos-generate-config --root /mnt; \
sed --in-place '/system\.stateVersion = .*/a \
nix.package = pkgs.nixUnstable;\n \
nix.extraOptions = \"experimental-features = nix-command flakes\";\n \
nix.settings.substituters = [\"https://mitchellh-nixos-config.cachix.org\"];\n \
nix.settings.trusted-public-keys = [\"mitchellh-nixos-config.cachix.org-1:bjEbXJyLrL1HZZHBbO4QALnI5faYZppzkU4D2s0G8RQ=\"];\n \
services.openssh.enable = true;\n \
services.openssh.settings.PasswordAuthentication = true;\n \
services.openssh.settings.PermitRootLogin = \"yes\";\n \
users.users.root.initialPassword = \"root\";\n \
' /mnt/etc/nixos/configuration.nix; \
nixos-install --no-root-passwd && reboot; \
"
# after bootstrap0, run this to finalize. After this, do everything else
# in the VM unless secrets change.
vm/bootstrap:
NIXUSER=root $(MAKE) vm/copy
NIXUSER=root $(MAKE) vm/switch
$(MAKE) vm/secrets
ssh $(SSH_OPTIONS) -p$(NIXPORT) $(NIXUSER)@$(NIXADDR) " \
sudo reboot; \
"
# copy our secrets into the VM
vm/secrets:
# GPG keyring
rsync -av -e 'ssh $(SSH_OPTIONS)' \
--exclude='.#*' \
--exclude='S.*' \
--exclude='*.conf' \
$(HOME)/.gnupg/ $(NIXUSER)@$(NIXADDR):~/.gnupg
# SSH keys
rsync -av -e 'ssh $(SSH_OPTIONS)' \
--exclude='environment' \
$(HOME)/.ssh/ $(NIXUSER)@$(NIXADDR):~/.ssh
# copy the Nix configurations into the VM.
vm/copy:
rsync -av -e 'ssh $(SSH_OPTIONS) -p$(NIXPORT)' \
--exclude='vendor/' \
--exclude='.git/' \
--exclude='.git-crypt/' \
--exclude='iso/' \
--rsync-path="sudo rsync" \
$(MAKEFILE_DIR)/ $(NIXUSER)@$(NIXADDR):/nix-config
# run the nixos-rebuild switch command. This does NOT copy files so you
# have to run vm/copy before.
vm/switch:
ssh $(SSH_OPTIONS) -p$(NIXPORT) $(NIXUSER)@$(NIXADDR) " \
sudo NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nixos-rebuild switch --flake \"/nix-config#${NIXNAME}\" \
"
# Build a WSL installer
.PHONY: wsl
wsl:
nix build ".#nixosConfigurations.wsl.config.system.build.installer"