1
0
Fork 0
mirror of https://github.com/ciderale/nixos-utm.git synced 2024-12-14 11:37:37 +00:00

automate setup process of utm nixos vm

- use proper disk image format (current 10G)
- change nixosIP to use mac addr => arp information
- add nixos-anywhere/nixos/disko configuration
- add script to remove iso to avoid boot order problems
This commit is contained in:
Alain Lehmann 2023-10-07 02:14:36 +02:00
parent 5af1292f42
commit b2f2d2c774
7 changed files with 316 additions and 4 deletions

32
configuration.nix Normal file
View file

@ -0,0 +1,32 @@
{
modulesPath,
config,
lib,
pkgs,
...
}: {
imports = [
(modulesPath + "/installer/scan/not-detected.nix")
#(modulesPath + "/profiles/qemu-guest.nix")
./disk-config.nix
./hardware-configuration.nix
];
boot.loader.grub = {
# no need to set devices, disko will add all devices that have a EF02 partition to the list already
# devices = [ ];
efiSupport = true;
efiInstallAsRemovable = true;
};
services.openssh.enable = true;
environment.systemPackages = map lib.lowPrio [
pkgs.curl
pkgs.gitMinimal
];
users.users.root.openssh.authorizedKeys.keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPKyKsE4eCn8BDnJZNmFttaCBmVUhO73qmhguEtNft6y alain.lehmann@gmail.com"
];
system.stateVersion = "23.11";
}

55
disk-config.nix Normal file
View file

@ -0,0 +1,55 @@
# Example to create a bios compatible gpt partition
{lib, ...}: {
disko.devices = {
disk.disk1 = {
device = lib.mkDefault "/dev/sda";
type = "disk";
content = {
type = "gpt";
partitions = {
boot = {
name = "boot";
size = "1M";
type = "EF02";
};
esp = {
name = "ESP";
size = "500M";
type = "EF00";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
name = "root";
size = "100%";
content = {
type = "lvm_pv";
vg = "pool";
};
};
};
};
};
lvm_vg = {
pool = {
type = "lvm_vg";
lvs = {
root = {
size = "100%FREE";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
mountOptions = [
"defaults"
];
};
};
};
};
};
};
}

BIN
empty.img.gz Normal file

Binary file not shown.

View file

@ -21,6 +21,48 @@
"type": "github"
}
},
"disko": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1696468923,
"narHash": "sha256-qSM7NKgf8LcZ5hjKHZ8ANFI8+LQivvAypbhJHBJmYFM=",
"owner": "nix-community",
"repo": "disko",
"rev": "cde886a1c97ef2399b4f91409db045785020291f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "disko",
"type": "github"
}
},
"disko_2": {
"inputs": {
"nixpkgs": [
"nixos-anywhere",
"nixpkgs"
]
},
"locked": {
"lastModified": 1694710969,
"narHash": "sha256-5nMY+3JF/ktW8D8URataP6MCbbnID13P4CixARNoq6w=",
"owner": "nix-community",
"repo": "disko",
"rev": "91af5b4a53ee8e57f4178ef58036dce49fbda91a",
"type": "github"
},
"original": {
"owner": "nix-community",
"ref": "master",
"repo": "disko",
"type": "github"
}
},
"flake-compat": {
"flake": false,
"locked": {
@ -54,6 +96,27 @@
"type": "indirect"
}
},
"flake-parts_2": {
"inputs": {
"nixpkgs-lib": [
"nixos-anywhere",
"nixpkgs"
]
},
"locked": {
"lastModified": 1693611461,
"narHash": "sha256-aPODl8vAgGQ0ZYFIRisxYG5MOGSkIczvu2Cd8Gb9+1Y=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "7f53fdb7bdc5bb237da7fefef12d099e4fd611ca",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
@ -134,6 +197,72 @@
"type": "github"
}
},
"nixos-2305": {
"locked": {
"lastModified": 1694724971,
"narHash": "sha256-NazxO2WuzV1pVnVoFpo7vJbgTTDhywhDxEDwFw6tyWY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "16a63df66bfefec64daab3da10d414055d782d66",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "release-23.05",
"repo": "nixpkgs",
"type": "github"
}
},
"nixos-anywhere": {
"inputs": {
"disko": "disko_2",
"flake-parts": "flake-parts_2",
"nixos-2305": "nixos-2305",
"nixos-images": "nixos-images",
"nixpkgs": [
"nixpkgs"
],
"treefmt-nix": "treefmt-nix"
},
"locked": {
"lastModified": 1696441224,
"narHash": "sha256-HYuoi7TSSHpRXW9wqCgxjPe9mEWnsAzIWIl3Z9oUA+k=",
"owner": "numtide",
"repo": "nixos-anywhere",
"rev": "ffcbf8c326f05f7a66563a12e57562f53af2c28b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "nixos-anywhere",
"type": "github"
}
},
"nixos-images": {
"inputs": {
"nixos-2305": [
"nixos-anywhere",
"nixos-2305"
],
"nixos-unstable": [
"nixos-anywhere",
"nixpkgs"
]
},
"locked": {
"lastModified": 1694429687,
"narHash": "sha256-fopTSMmOjk7/ro8+P2wZxAC8OPUKtuwHLNp2xSPXVjc=",
"owner": "nix-community",
"repo": "nixos-images",
"rev": "c4c73bce65306a1e747684dd0d4bcf0ab2779585",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixos-images",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1678875422,
@ -247,7 +376,9 @@
"root": {
"inputs": {
"devenv": "devenv",
"disko": "disko",
"flake-parts": "flake-parts",
"nixos-anywhere": "nixos-anywhere",
"nixpkgs": "nixpkgs_2"
}
},
@ -265,6 +396,27 @@
"repo": "default",
"type": "github"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": [
"nixos-anywhere",
"nixpkgs"
]
},
"locked": {
"lastModified": 1694528738,
"narHash": "sha256-aWMEjib5oTqEzF9f3WXffC1cwICo6v/4dYKjwNktV8k=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "7a49c388d7a6b63bb551b1ddedfa4efab8f400d8",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",

View file

@ -4,6 +4,10 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
devenv.url = "github:cachix/devenv";
disko.url = "github:nix-community/disko";
disko.inputs.nixpkgs.follows = "nixpkgs";
nixos-anywhere.url = "github:numtide/nixos-anywhere";
nixos-anywhere.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs @ {flake-parts, ...}:
@ -59,7 +63,12 @@
name = "nixosIP";
runtimeInputs = [self'.packages.nixosCmd pkgs.gnused];
text = ''
nixosCmd ip a | sed -ne 's/.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*scope global.*/\1/p'
MAC=$(sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' "$UTM_DATA_DIR/$NIXOS_NAME.utm/config.plist")
# shellcheck disable=SC2001
MAC1=$(sed -e 's/0\([[:digit:]]\)/\1/' <<< "$MAC")
IP=$(arp -a | sed -ne "s/.*(\([0-9.]*\)) at $MAC1.*/\1/p")
echo "$IP"
#nixosCmd ip a | sed -ne 's/.*inet \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*scope global.*/\1/p'
'';
};
packages.nixosSetRootPW = pkgs.writeShellApplication {
@ -77,7 +86,13 @@
};
packages.nixosCreate = pkgs.writeShellApplication {
name = "nixosCreate";
runtimeInputs = [pkgs.util-linux.bin pkgs.coreutils pkgs.gnused self'.packages.nixosCmd];
runtimeInputs = [
pkgs.util-linux.bin
pkgs.coreutils
pkgs.gnused
self'.packages.nixosCmd
inputs'.nixos-anywhere.packages.default
];
text = ''
UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents";
@ -90,7 +105,8 @@
FOLDER="$UTM_DATA_DIR/$NAME.utm"
mkdir -p "$FOLDER/Data"
touch "$FOLDER/Data/$DISK_ID.img"
tar xvzf ${./empty.img.gz}
mv empty.img "$FOLDER/Data/$DISK_ID.img"
install -m 600 ${./efi_vars.fd} "$FOLDER/Data/efi_vars.fd"
sed -e "s/XXX_NAME/$NAME/g;s/XXX_VM_ID/$VM_ID/g;s/XXX_DISK_ID/$DISK_ID/g;s/XXX_MAC_ADDR/$MAC_ADDR/g" ${./config.plist} > "$FOLDER/config.plist"
@ -108,10 +124,21 @@
echo "setting password"
nixosCmd "echo -e '$NIXOS_PW\n$NIXOS_PW' | sudo passwd"
nixosCmd "sudo mkdir -p /root/.ssh"
nixosCmd "echo '${builtins.head inputs.self.nixosConfigurations.utm.config.users.users.root.openssh.authorizedKeys.keys}' | sudo tee -a /root/.ssh/authorized_keys"
#echo "$NIXOS_PW"
#ssh-copy-id "root@$(nixosIP)"
nixos-anywhere --flake .#utm "root@$(nixosIP)" --build-on-remote
utmctl stop "$NAME"
osascript ${./removeIso.osa} "$NAME"
utmctl start "$NAME"
'';
};
devenv.shells.default = {
env.NIXOS_NAME = "AAA";
env.NIXOS_NAME = "MyNixOS";
env.NIXOS_PW = "foo";
enterShell = ''
export UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents";
@ -119,6 +146,7 @@
packages = builtins.attrValues {
inherit (self'.packages) utm sshNixos nixosIP nixosCmd nixosSetRootPW nixosCreate;
inherit (pkgs) coreutils expect;
aw = inputs'.nixos-anywhere.packages.default;
};
};
};
@ -126,6 +154,14 @@
# The usual flake attributes can be defined here, including system-
# agnostic ones like nixosModule and system-enumerating ones, although
# those are more easily expressed in perSystem.
nixosConfigurations.utm = inputs.nixpkgs.lib.nixosSystem {
system = "aarch64-linux";
modules = [
inputs.disko.nixosModules.disko
{disko.devices.disk.disk1.device = "/dev/vda";}
./configuration.nix
];
};
};
};
}

View file

@ -0,0 +1,26 @@
# Do not modify this file! It was generated by nixos-generate-config
# and may be overwritten by future invocations. Please make changes
# to /etc/nixos/configuration.nix instead.
{
config,
lib,
pkgs,
modulesPath,
...
}: {
imports = [];
boot.initrd.availableKernelModules = ["virtio_pci" "xhci_pci" "usb_storage" "usbhid"];
boot.initrd.kernelModules = [];
boot.kernelModules = [];
boot.extraModulePackages = [];
# Enables DHCP on each ethernet and wireless interface. In case of scripted networking
# (the default) this is the recommended approach. When using systemd-networkd it's
# still possible to use this option, but it's recommended to use it in conjunction
# with explicit per-interface declarations with `networking.interfaces.<interface>.useDHCP`.
networking.useDHCP = lib.mkDefault true;
# networking.interfaces.enp0s1.useDHCP = lib.mkDefault true;
nixpkgs.hostPlatform = lib.mkDefault "aarch64-linux";
}

11
removeIso.osa Normal file
View file

@ -0,0 +1,11 @@
on run argv
set vm_name to item 1 of argv
tell application "UTM"
set vm to virtual machine named vm_name
set config to configuration of vm
set mydrives to drives of config
set newdrives to (item 1 of mydrives)
set drives of config to newdrives
update configuration of vm with config
end tell
end run