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

add UTM_CONFIG environment variable for VM config

this allows to define a custom nix configuration file
to set the UTM VM configuration parameters. The content
of this nix file is merged with the config.plist of the VM.
The merging is done using json representation.
This commit is contained in:
Alain Lehmann 2024-10-11 01:11:47 +02:00
parent 3a57708f4e
commit 140ee090f5
3 changed files with 76 additions and 17 deletions

View file

@ -18,6 +18,8 @@ automate as much as possible for a simple automation setup.
- adjust ./configuration.nix (atleast set your ssh public key) - adjust ./configuration.nix (atleast set your ssh public key)
- define an environment variable VM_NAME to name the UTM VM - define an environment variable VM_NAME to name the UTM VM
- maybe configure this env var within your project/system setup - maybe configure this env var within your project/system setup
- (optionally) define an environment variable UTM_CONFIG to set UTM VM parameters
- fallback to UTM_CONFIG=./utm.config.nix (which is merged using plutil)
- kickoff the installation process with the following command - kickoff the installation process with the following command
- assuming your flake is the current directory `.` with config `utm` - assuming your flake is the current directory `.` with config `utm`

View file

@ -60,10 +60,9 @@
}; };
packages.nixosIP = pkgs.writeShellApplication { packages.nixosIP = pkgs.writeShellApplication {
name = "nixosIP"; name = "nixosIP";
runtimeInputs = [self'.packages.nixosCmd pkgs.gnused]; runtimeInputs = [self'.packages.utmConfiguration pkgs.gnused];
text = '' text = ''
UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents"; MAC=$(utmConfiguration mac)
MAC=$(sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' "$UTM_DATA_DIR/$VM_NAME.utm/config.plist")
# shellcheck disable=SC2001 # shellcheck disable=SC2001
MAC1=$(sed -e 's/0\([[:digit:]]\)/\1/g' <<< "$MAC") MAC1=$(sed -e 's/0\([[:digit:]]\)/\1/g' <<< "$MAC")
IP= IP=
@ -121,12 +120,17 @@
self'.packages.nixosCmd self'.packages.nixosCmd
self'.packages.nixosIP self'.packages.nixosIP
self'.packages.killUTM self'.packages.killUTM
self'.packages.utmConfiguration
inputs'.nixos-anywhere.packages.default inputs'.nixos-anywhere.packages.default
]; ];
text = '' text = ''
set -x set -x
FLAKE_CONFIG=''${1:-".#utm"} FLAKE_CONFIG=''${1:-".#utm"}
shift 1 shift 1
# define UTM_CONFIG -- or fallback to provided default
: "''${UTM_CONFIG:=${./utm.config.nix}}"
echo "## Check that the provided nixosConfiguration $FLAKE_CONFIG exists" echo "## Check that the provided nixosConfiguration $FLAKE_CONFIG exists"
nix eval "''${FLAKE_CONFIG/'#'/'#'nixosConfigurations.}.config.system.stateVersion" nix eval "''${FLAKE_CONFIG/'#'/'#'nixosConfigurations.}.config.system.stateVersion"
@ -146,19 +150,7 @@
osascript ${./setupVM.osa} "$VM_NAME" "$MAC_ADDR" ${self'.packages.nixosImg} osascript ${./setupVM.osa} "$VM_NAME" "$MAC_ADDR" ${self'.packages.nixosImg}
echo "configure the VM with plutil" echo "configure the VM with plutil"
UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents"; utmConfiguration update "$UTM_CONFIG"
FOLDER="$UTM_DATA_DIR/$VM_NAME.utm"
CFG="$FOLDER"/config.plist
plutil -insert "Display.0" -json '{ "HeightPixels": 1200, "PixelsPerInch": 226, "WidthPixels": 1920 }' "$CFG"
plutil -replace "Virtualization.Rosetta" -bool true "$CFG"
plutil -replace "Virtualization.Keyboard" -bool true "$CFG"
plutil -replace "Virtualization.Trackpad" -bool true "$CFG"
plutil -replace "Virtualization.Pointer" -bool true "$CFG"
plutil -replace "Virtualization.Keybaord" -bool true "$CFG"
plutil -replace "Virtualization.ClipboardSharing" -bool true "$CFG"
plutil -replace "Virtualization.Audio" -bool true "$CFG"
plutil -replace "Virtualization.Balloon" -bool true "$CFG"
echo -e "\n\n## refresh UTMs view of the configuration requires restarting UTM" echo -e "\n\n## refresh UTMs view of the configuration requires restarting UTM"
killUTM killUTM
@ -189,6 +181,52 @@
ssh-keygen -R "$(nixosIP)" ssh-keygen -R "$(nixosIP)"
''; '';
}; };
packages.utmConfiguration = pkgs.writeShellApplication {
name = "utmConfiguration";
runtimeInputs = [pkgs.jq];
text = ''
# INPUTS: VM_NAME
UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents";
VM_FOLDER="$UTM_DATA_DIR/$VM_NAME.utm"
PLIST_FILE="$VM_FOLDER"/config.plist
CMD=$1
shift
case "$CMD" in
show)
plutil -convert json -o - "$PLIST_FILE" | jq .
;;
mac)
$0 show | jq '.Network[0].MacAddress' -r
;;
update)
NIX_PATCH=$1
# CREATE TEMPORARY FILES
PLIST_JSON=$(mktemp -u)
PLIST_JSON_PLIST=$(mktemp -u)
JSON_PATCH=$(mktemp -u)
trap 'rm $PLIST_JSON $PLIST_JSON_PLIST $JSON_PATCH' EXIT
# EXECUTE UPDATE
nix eval -f "$NIX_PATCH" --json > "$JSON_PATCH"
plutil -convert json -o "$PLIST_JSON" "$PLIST_FILE"
jq -s 'reduce .[] as $item ({}; . * $item)' "$PLIST_JSON" "$JSON_PATCH" > "$PLIST_JSON_PLIST"
plutil -convert binary1 "$PLIST_JSON_PLIST"
# WRITE UPDATE TO CONFIG
cp "$PLIST_JSON_PLIST" "$PLIST_FILE"
;;
*)
echo "usage: VM_NAME=your-vm $0 show "
echo "usage: VM_NAME=your-vm $0 update patch-config.nix"
;;
esac
'';
};
packages.nixosDeploy = pkgs.writeShellApplication { packages.nixosDeploy = pkgs.writeShellApplication {
name = "nixosDeploy"; name = "nixosDeploy";
runtimeInputs = [ runtimeInputs = [
@ -223,7 +261,7 @@
export UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents"; export UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents";
''; '';
packages = builtins.attrValues { packages = builtins.attrValues {
inherit (self'.packages) nixosCreate sshNixos utm nixosCmd; inherit (self'.packages) nixosCreate sshNixos utm nixosCmd utmConfiguration nixosIP;
inherit (pkgs) coreutils nixos-rebuild; inherit (pkgs) coreutils nixos-rebuild;
inherit (inputs'.nixos-anywhere.packages) nixos-anywhere; inherit (inputs'.nixos-anywhere.packages) nixos-anywhere;
}; };

19
utm.config.nix Normal file
View file

@ -0,0 +1,19 @@
{
Display = [
{
HeightPixels = 1200;
WidthPixels = 1920;
PixelsPerInch = 226;
}
];
System = {MemorySize = 4096;};
Virtualization = {
Audio = true;
Balloon = true;
ClipboardSharing = true;
Keyboard = true;
Pointer = true;
Rosetta = true;
Trackpad = true;
};
}