Nixos support (#4)

* build nixos

Signed-off-by: Christine Dodrill <me@christine.website>

* does this work lol

Signed-off-by: Christine Dodrill <me@christine.website>

* oops

Signed-off-by: Christine Dodrill <me@christine.website>

* openstack?

Signed-off-by: Christine Dodrill <me@christine.website>

* oops lol

Signed-off-by: Christine Dodrill <me@christine.website>

* write a fake cloud config i guess

Signed-off-by: Christine Dodrill <me@christine.website>

* enable ssh lol

Signed-off-by: Christine Dodrill <me@christine.website>

* openstack is banned

Signed-off-by: Christine Dodrill <me@christine.website>

* lol oops x3

Signed-off-by: Christine Dodrill <me@christine.website>

* enable cloud init

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-05-17 21:34:09 -04:00 committed by GitHub
parent 2e32a4e4bf
commit 62200eddd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 12 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.qcow2

56
main.go
View File

@ -44,6 +44,16 @@ func main() {
rand.Seed(time.Now().Unix())
flag.Parse()
cdir, err := os.UserCacheDir()
if err != nil {
log.Fatalf("can't find cache dir: %v", err)
}
cdir = filepath.Join(cdir, "within", "mkvm")
os.MkdirAll(filepath.Join(cdir, "nixos"), 0755)
os.MkdirAll(filepath.Join(cdir, "qcow2"), 0755)
os.MkdirAll(filepath.Join(cdir, "seed"), 0755)
vmID := uuid.New().String()
if *name == "" {
commonBladeName, err := getName()
if err != nil {
@ -59,6 +69,18 @@ func main() {
var resultDistro Distro
var found bool
qcowPath := filepath.Join(cdir, "nixos", vmID, "nixos.qcow2")
if *distro == "nixos" {
found = true
resultDistro = Distro{
Name: "nixos",
DownloadURL: "file://" + qcowPath,
Sha256Sum: "<computed after build>",
MinSize: 8,
}
}
for _, d := range distros {
if d.Name == *distro {
found = true
@ -78,7 +100,11 @@ func main() {
}
os.Exit(1)
}
zvol := filepath.Join(*zvolPrefix, *name)
if resultDistro.Name != "nixos" {
qcowPath = filepath.Join(cdir, "qcow2", resultDistro.Sha256Sum)
}
macAddress, err := randomMac()
if err != nil {
@ -90,8 +116,6 @@ func main() {
log.Fatalf("can't connect to libvirt: %v", err)
}
vmID := uuid.New().String()
log.Println("plan:")
log.Printf("name: %s", *name)
log.Printf("zvol: %s (%d GB)", zvol, *zvolSize)
@ -108,14 +132,13 @@ func main() {
fmt.Print("press enter if this looks okay: ")
reader.ReadString('\n')
cdir, err := os.UserCacheDir()
if err != nil {
log.Fatalf("can't find cache dir: %v", err)
if *distro == "nixos" {
_, err := mkNixOSImage(*cloudConfig, cdir, vmID)
if err != nil {
log.Fatal(err)
}
}
cdir = filepath.Join(cdir, "within", "mkvm")
os.MkdirAll(filepath.Join(cdir, "qcow2"), 0755)
os.MkdirAll(filepath.Join(cdir, "seed"), 0755)
qcowPath := filepath.Join(cdir, "qcow2", resultDistro.Sha256Sum)
_, err = os.Stat(qcowPath)
if err != nil {
log.Printf("downloading distro image %s to %s", resultDistro.DownloadURL, qcowPath)
@ -189,9 +212,18 @@ func main() {
}
fout.Close()
err = run("cp", *cloudConfig, filepath.Join(dir, "user-data"))
if err != nil {
log.Fatal(err)
if *distro != "nixos" {
err = run("cp", *cloudConfig, filepath.Join(dir, "user-data"))
if err != nil {
log.Fatal(err)
}
} else {
fout, err := os.Create(filepath.Join(dir, "user-data"))
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(fout, "#cloud-config")
fout.Close()
}
isoPath := filepath.Join(cdir, "seed", fmt.Sprintf("%s-%s.iso", *name, vmID))

13
nixos.go Normal file
View File

@ -0,0 +1,13 @@
package main
import "path/filepath"
func mkNixOSImage(configFname, cacheDir, vmID string) (string, error) {
outputFname := filepath.Join(cacheDir, "nixos", vmID)
err := run("nix-shell", "-p", "nixos-generators", "--run", "nixos-generate -f qcow -o "+outputFname+" -c "+configFname)
if err != nil {
return "", err
}
return outputFname, nil
}

28
var/xe-base.nix Normal file
View File

@ -0,0 +1,28 @@
{ config, pkgs, modulesPath, ... }:
{
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
boot.initrd.availableKernelModules =
[ "ata_piix" "uhci_hcd" "virtio_pci" "sr_mod" "virtio_blk" ];
boot.initrd.kernelModules = [ ];
boot.kernelModules = [ ];
boot.extraModulePackages = [ ];
users.users.xe = {
isNormalUser = true;
initialPassword = "hunter2";
extraGroups = [ "wheel" ];
openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPg9gYKVglnO2HQodSJt4z4mNrUSUiyJQ7b+J798bwD9"
];
};
services.openssh.enable = true;
security.sudo.wheelNeedsPassword = false;
services.cloud-init = {
enable = true;
ext4.enable = true;
};
}