2023-10-06 19:43:38 +00:00
|
|
|
{
|
|
|
|
description = "Description for the project";
|
|
|
|
|
|
|
|
inputs = {
|
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
|
|
devenv.url = "github:cachix/devenv";
|
2023-10-07 00:14:36 +00:00
|
|
|
disko.url = "github:nix-community/disko";
|
|
|
|
disko.inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
nixos-anywhere.url = "github:numtide/nixos-anywhere";
|
|
|
|
nixos-anywhere.inputs.nixpkgs.follows = "nixpkgs";
|
2023-10-06 19:43:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
outputs = inputs @ {flake-parts, ...}:
|
|
|
|
flake-parts.lib.mkFlake {inherit inputs;} {
|
|
|
|
imports = [
|
|
|
|
inputs.devenv.flakeModule
|
|
|
|
];
|
|
|
|
systems = ["x86_64-linux" "aarch64-darwin"];
|
|
|
|
perSystem = {
|
|
|
|
config,
|
|
|
|
self',
|
|
|
|
inputs',
|
|
|
|
pkgs,
|
|
|
|
system,
|
|
|
|
...
|
|
|
|
}: {
|
|
|
|
# Per-system attributes can be defined here. The self' and inputs'
|
|
|
|
# module parameters provide easy access to attributes of the same
|
|
|
|
# system.
|
|
|
|
# Equivalent to inputs'.nixpkgs.legacyPackages.hello;
|
|
|
|
packages.nixosImg = pkgs.fetchurl {
|
|
|
|
url = "https://hydra.nixos.org/build/237110262/download/1/nixos-minimal-23.11pre531102.fdd898f8f79e-aarch64-linux.iso";
|
|
|
|
sha256 = "sha256-PF6EfDXHJDQHHHN+fXUKBcRIRszvpQrrWmIyltFHn5c=";
|
|
|
|
};
|
|
|
|
packages.utm = pkgs.utm.overrideAttrs (oldAttrs: rec {
|
|
|
|
version = "4.4.2";
|
|
|
|
src = pkgs.fetchurl {
|
|
|
|
url = "https://github.com/utmapp/UTM/releases/download/v${version}/UTM.dmg";
|
|
|
|
#hash = "sha256-aDIjf4TqhSIgYaJulI5FgXxlNiZ1qcNY+Typ7+S5Hc8=";
|
|
|
|
hash = "sha256-QKZNIqJpY5ipl6R5/UHjfh6I5NkyFn5xZLy/CL5453g=";
|
|
|
|
};
|
|
|
|
});
|
|
|
|
packages.nixosCmd = pkgs.writeShellApplication {
|
|
|
|
name = "nixosCmd";
|
|
|
|
runtimeInputs = [self'.packages.utm];
|
|
|
|
text = ''
|
|
|
|
TT=$(utmctl attach "$NIXOS_NAME" | sed -n -e 's/PTTY: //p')
|
2023-10-09 20:39:45 +00:00
|
|
|
echo "TTY IS: $TT"
|
2023-10-06 19:43:38 +00:00
|
|
|
DAT=/tmp/ttyDump.dat.''$''$
|
|
|
|
trap 'rm "$DAT"' EXIT
|
|
|
|
|
|
|
|
exec 3<"$TT" #REDIRECT SERIAL OUTPUT TO FD 3
|
|
|
|
cat <&3 > "$DAT" & #REDIRECT SERIAL OUTPUT TO FILE
|
|
|
|
PID=$! #SAVE PID TO KILL CAT
|
|
|
|
echo -e "$@" > "$TT";
|
|
|
|
sleep 0.3s #WAIT FOR RESPONSE
|
|
|
|
kill $PID #KILL CAT PROCESS
|
|
|
|
wait $PID 2>/dev/null || true #SUPRESS "Terminated" output
|
|
|
|
exec 3<&-
|
|
|
|
cat $DAT
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
packages.nixosIP = pkgs.writeShellApplication {
|
|
|
|
name = "nixosIP";
|
|
|
|
runtimeInputs = [self'.packages.nixosCmd pkgs.gnused];
|
|
|
|
text = ''
|
2023-10-07 00:14:36 +00:00
|
|
|
MAC=$(sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p' "$UTM_DATA_DIR/$NIXOS_NAME.utm/config.plist")
|
|
|
|
# shellcheck disable=SC2001
|
2023-10-15 14:47:26 +00:00
|
|
|
MAC1=$(sed -e 's/0\([[:digit:]]\)/\1/g' <<< "$MAC")
|
2023-10-07 00:14:36 +00:00
|
|
|
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'
|
2023-10-06 19:43:38 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
packages.nixosSetRootPW = pkgs.writeShellApplication {
|
|
|
|
name = "nixosSetRootPW";
|
|
|
|
runtimeInputs = [self'.packages.nixosCmd];
|
2023-10-15 16:29:23 +00:00
|
|
|
text = ''NIXOS_PW=$1; nixosCmd "echo -e '$NIXOS_PW\n$NIXOS_PW' | sudo passwd" '';
|
2023-10-06 19:43:38 +00:00
|
|
|
};
|
|
|
|
packages.sshNixos = pkgs.writeShellApplication {
|
|
|
|
name = "sshNixos";
|
|
|
|
runtimeInputs = [self'.packages.nixosIP pkgs.openssh];
|
|
|
|
text = ''
|
|
|
|
# shellcheck disable=SC2029
|
2023-10-09 19:38:20 +00:00
|
|
|
ssh "root@$(nixosIP)" "$@"
|
2023-10-06 19:43:38 +00:00
|
|
|
'';
|
|
|
|
};
|
2023-10-15 16:00:17 +00:00
|
|
|
packages.killUTM = pkgs.writeShellApplication {
|
|
|
|
name = "killUTM";
|
2023-10-15 17:25:47 +00:00
|
|
|
runtimeInputs = [
|
|
|
|
self'.packages.utm
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.gnused
|
|
|
|
pkgs.ps
|
|
|
|
];
|
2023-10-15 16:00:17 +00:00
|
|
|
text = ''
|
|
|
|
# shellcheck disable=SC2009
|
|
|
|
if ps aux | grep '/[U]TM'; then
|
2023-10-15 17:25:47 +00:00
|
|
|
UTM_PID=$(ps ax -o pid,command | grep '/[U]TM'| sed -ne 's/^[ ]*\([[:digit:]]*\) .*/\1/p')
|
2023-10-15 16:00:17 +00:00
|
|
|
read -r -e -p "Running at $UTM_PID. Kill? (y/N)" -i "n" answer
|
|
|
|
case "$answer" in
|
|
|
|
y | Y | yes ) kill "$UTM_PID" ;;
|
|
|
|
*) echo "don't stop UTM. abort."; exit ;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
2023-10-06 21:03:45 +00:00
|
|
|
packages.nixosCreate = pkgs.writeShellApplication {
|
|
|
|
name = "nixosCreate";
|
2023-10-07 00:14:36 +00:00
|
|
|
runtimeInputs = [
|
|
|
|
pkgs.util-linux.bin
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.gnused
|
2023-10-09 19:38:20 +00:00
|
|
|
pkgs.openssh
|
2023-10-09 20:39:45 +00:00
|
|
|
pkgs.ps
|
2023-10-09 20:49:23 +00:00
|
|
|
self'.packages.utm
|
2023-10-07 00:14:36 +00:00
|
|
|
self'.packages.nixosCmd
|
2023-10-09 20:49:23 +00:00
|
|
|
self'.packages.nixosIP
|
2023-10-15 16:00:17 +00:00
|
|
|
self'.packages.killUTM
|
2023-10-07 00:14:36 +00:00
|
|
|
inputs'.nixos-anywhere.packages.default
|
|
|
|
];
|
2023-10-06 21:03:45 +00:00
|
|
|
text = ''
|
|
|
|
UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents";
|
|
|
|
|
2023-10-15 16:29:23 +00:00
|
|
|
FLAKE_CONFIG=".#utm"
|
2023-10-06 21:03:45 +00:00
|
|
|
NAME=$NIXOS_NAME
|
2023-10-15 16:29:23 +00:00
|
|
|
AUTHORIZED_PATH="config.users.users.root.openssh.authorizedKeys.keys"
|
|
|
|
AUTHORIZED_KEYS=$(
|
|
|
|
nix eval --raw --apply 'builtins.concatStringsSep "\n"' \
|
|
|
|
"''${FLAKE_CONFIG/'#'/#nixosConfigurations.}.''${AUTHORIZED_PATH}"
|
|
|
|
)
|
|
|
|
echo "$AUTHORIZED_KEYS"
|
|
|
|
|
2023-10-06 21:03:45 +00:00
|
|
|
#MAC_ADDR=$(tr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/\1:/g;s/:$//;s/^/02:/')
|
|
|
|
MAC_ADDR=$(md5sum <<< "$NAME" | head -c 10 | sed -r 's/(..)/\1:/g;s/:$//;s/^/02:/')
|
|
|
|
|
|
|
|
|
2023-10-15 16:00:17 +00:00
|
|
|
if utmctl list | grep "$NAME" ; then
|
|
|
|
read -r -e -p "The VM [$NAME] exists: should the VM be deleted (y/N)" -i "n" answer
|
2023-10-09 20:39:45 +00:00
|
|
|
case "$answer" in
|
2023-10-15 16:00:17 +00:00
|
|
|
y | Y | yes ) utmctl stop "$NAME"; utmctl delete "$NAME" ;;
|
2023-10-09 20:39:45 +00:00
|
|
|
*) echo "keep existing VM. abort."; exit ;;
|
|
|
|
esac
|
|
|
|
fi
|
2023-10-06 21:03:45 +00:00
|
|
|
|
2023-10-15 16:00:17 +00:00
|
|
|
echo "create the VM [$NAME] with applescript"
|
2023-10-15 14:44:45 +00:00
|
|
|
osascript ${./setupVM.osa} "$NAME" "$MAC_ADDR" ${self'.packages.nixosImg}
|
2023-10-09 20:39:45 +00:00
|
|
|
sleep 2 # sometimes iso is not recognised.. maybe sleep helps
|
|
|
|
|
2023-10-15 16:00:17 +00:00
|
|
|
echo "configure the VM with plutil"
|
|
|
|
FOLDER="$UTM_DATA_DIR/$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 false "$CFG"
|
|
|
|
plutil -replace "Virtualization.Balloon" -bool true "$CFG"
|
|
|
|
echo "refresh UTMs view of the configuration"
|
|
|
|
killUTM
|
|
|
|
|
2023-10-06 21:03:45 +00:00
|
|
|
utmctl start "$NAME"
|
|
|
|
while ! nixosCmd ls | grep nixos ; do
|
|
|
|
echo "VM $NAME not yet running"
|
|
|
|
sleep 2;
|
|
|
|
done
|
|
|
|
nixosCmd uname
|
|
|
|
echo "VM $NAME is running"
|
|
|
|
|
|
|
|
echo "setting password"
|
2023-10-07 00:14:36 +00:00
|
|
|
nixosCmd "sudo mkdir -p /root/.ssh"
|
2023-10-15 16:29:23 +00:00
|
|
|
nixosCmd "echo '$AUTHORIZED_KEYS' | sudo tee -a /root/.ssh/authorized_keys"
|
2023-10-09 20:39:45 +00:00
|
|
|
sleep 2
|
2023-10-07 00:14:36 +00:00
|
|
|
|
2023-10-15 16:29:23 +00:00
|
|
|
nixos-anywhere --flake "''${FLAKE_CONFIG}" "root@$(nixosIP)" --build-on-remote
|
2023-10-07 00:14:36 +00:00
|
|
|
|
|
|
|
utmctl stop "$NAME"
|
|
|
|
osascript ${./removeIso.osa} "$NAME"
|
|
|
|
utmctl start "$NAME"
|
2023-10-09 19:38:20 +00:00
|
|
|
|
2023-10-09 20:39:45 +00:00
|
|
|
while ! ssh-keyscan "$(nixosIP)"; do sleep 2; done
|
2023-10-09 19:38:20 +00:00
|
|
|
ssh-keygen -R "$(nixosIP)"
|
2023-10-06 21:03:45 +00:00
|
|
|
'';
|
|
|
|
};
|
2023-10-06 19:43:38 +00:00
|
|
|
devenv.shells.default = {
|
2023-10-15 14:44:45 +00:00
|
|
|
env.NIXOS_NAME = "MyNixOS2";
|
2023-10-06 21:03:45 +00:00
|
|
|
enterShell = ''
|
|
|
|
export UTM_DATA_DIR="$HOME/Library/Containers/com.utmapp.UTM/Data/Documents";
|
|
|
|
'';
|
2023-10-06 19:43:38 +00:00
|
|
|
packages = builtins.attrValues {
|
2023-10-15 14:23:13 +00:00
|
|
|
inherit (self'.packages) nixosCreate sshNixos utm;
|
2023-10-06 19:43:38 +00:00
|
|
|
inherit (pkgs) coreutils expect;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
flake = {
|
|
|
|
# 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.
|
2023-10-07 00:14:36 +00:00
|
|
|
nixosConfigurations.utm = inputs.nixpkgs.lib.nixosSystem {
|
|
|
|
system = "aarch64-linux";
|
|
|
|
modules = [
|
|
|
|
inputs.disko.nixosModules.disko
|
|
|
|
{disko.devices.disk.disk1.device = "/dev/vda";}
|
|
|
|
./configuration.nix
|
|
|
|
];
|
|
|
|
};
|
2023-10-06 19:43:38 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|