-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
103 lines (84 loc) · 2.94 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"regexp"
"strings"
)
const zfs = "zfs"
const ext4 = "ext4"
func check(err error) {
if err != nil {
log.Panic(err)
}
}
func execute(cmdName string, args ...string) {
cmd := exec.Command(cmdName, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
check(cmd.Run())
}
func main() {
compression := flag.Bool("compress", true, "use compression on ZFS pool")
encryption := flag.Bool("encrypt", false, "use encryption on ZFS pool")
rootFileSystem := flag.String("fs", zfs, "filesystem to use on root, currently ext4 and zfs")
targetDevice := flag.String("device", "", "Device to use ")
flag.Parse()
if *targetDevice == "" {
fmt.Print("Please provide device name via -device flag")
os.Exit(1)
}
rootPartition := *targetDevice + "1"
swapPartition := *targetDevice + "2"
bootPartition := *targetDevice + "3"
if strings.HasPrefix(*targetDevice, "/dev/nvme") {
rootPartition = *targetDevice + "p1"
swapPartition = *targetDevice + "p2"
bootPartition = *targetDevice + "p3"
}
execute("parted", *targetDevice, "--", "mklabel", "gpt")
execute("parted", *targetDevice, "--", "mkpart", "primary", "512MiB", "-8GiB")
execute("parted", *targetDevice, "--", "mkpart", "primary", "linux-swap", "-8GiB", "100%")
execute("parted", *targetDevice, "--", "mkpart", "ESP", "fat32", "1MiB", "512MiB")
execute("parted", *targetDevice, "--", "set", "3", "esp", "on")
if *rootFileSystem == ext4 {
execute("mkfs.ext4", rootPartition)
execute("mount", rootPartition, "/mnt")
} else if *rootFileSystem == zfs {
zfsPoolName := "zroot"
nixosZfsDataset := path.Join(zfsPoolName, "root")
createArgs := []string{
"create", "-f", "-O", "mountpoint=none", "-O", "atime=off",
}
if *compression {
createArgs = append(createArgs, "-O", "compression=zstd")
}
if *encryption {
createArgs = append(createArgs, "-O", "encryption=aes-256-gcm")
createArgs = append(createArgs, "-O", "keyformat=passphrase")
}
createArgs = append(createArgs, "-O", "xattr=sa", "-O", "acltype=posixacl", "-o", "autotrim=on", "-o", "ashift=12", "-R", "/mnt", zfsPoolName, rootPartition)
execute("zpool", createArgs...)
execute("zfs", "create", "-o", "mountpoint=legacy", nixosZfsDataset)
execute("mount", "-t", "zfs", nixosZfsDataset, "/mnt")
}
execute("mkswap", swapPartition)
execute("mkfs.fat", "-F", "32", "-n", "boot", bootPartition)
execute("mkdir", "-p", "/mnt/boot")
execute("mount", bootPartition, "/mnt/boot")
execute("swapon", swapPartition)
execute("nixos-generate-config", "--root", "/mnt")
configFilePath := "/mnt/etc/nixos/configuration.nix"
content, err := os.ReadFile(configFilePath)
check(err)
regex := regexp.MustCompile("\n{\n")
newConfig := regex.ReplaceAllString(string(content), "\n{\n networking.hostId = \"00000000\";\n")
//TODO correct permissions
check(os.WriteFile(configFilePath, []byte(newConfig), os.ModePerm))
execute("nixos-install")
}