From 9bb76c2ee01e8e65966bb25a725d1c3dbf61420c Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 19 Dec 2023 12:51:26 -0300 Subject: [PATCH] refact(recover): refact into machine module --- flake.nix | 78 +++++++++----------- machines/default.nix | 4 + machines/recover/default.nix | 138 +++++++++++++++++++++++++++++++++++ recover.nix | 117 ----------------------------- 4 files changed, 176 insertions(+), 161 deletions(-) create mode 100644 machines/default.nix create mode 100644 machines/recover/default.nix delete mode 100644 recover.nix diff --git a/flake.nix b/flake.nix index 84426a5..c49d4b3 100644 --- a/flake.nix +++ b/flake.nix @@ -11,63 +11,53 @@ os = "linux"; system = "${platform}-${os}"; pkgs = import inputs.nixpkgs { inherit system; }; - mkSystem = options: - let - inherit (inputs.nixpkgs.lib) nixosSystem; - inherit (inputs.generators) nixosGenerate; - inherit (builtins) hasAttr; - in - if hasAttr "format" options then - nixosGenerate options - else - nixosSystem options; - recover = mkSystem { - inherit system; - modules = [ ./recover.nix ]; + lib = { + mkSystem = options: + let + inherit (inputs.nixpkgs.lib) nixosSystem; + inherit (inputs.generators) nixosGenerate; + inherit (builtins) hasAttr; + in + if hasAttr "format" options then + nixosGenerate options + else + nixosSystem options; + # TODO: find a faster way to run vm in devShell + mkRecoverVm = efi: name: args: pkgs.writeShellApplication { + name = "${name}-vm"; + text = '' + IMG="${name}-efi.img" + BIOS="${name}-efi-bios.img" + ARGS="${args}" + cp -ui --reflink=auto ${pkgs.OVMF.fd}/FV/OVMF.fd "$BIOS" + chmod a+w "$BIOS" + cp -ui --reflink=auto ${efi}/nixos.img "$IMG" + chmod a+w "$IMG" + qemu-system-${platform} \ + -bios "$BIOS" \ + -drive file="$IMG",format=raw \ + -m 2G \ + $ARGS + ''; + runtimeInputs = with pkgs; [ qemu ]; + }; }; - - recover-efi = mkSystem { - inherit system; - modules = [ ./recover.nix ]; - format = "raw-efi"; - }; - - # TODO: find a faster way to run recover in devShell - mk-recover-vm = args: pkgs.writeShellApplication { - name = "recover-vm"; - text = '' - IMG="recover-efi.img" - BIOS="recover-efi-bios.img" - ARGS="${args}" - cp -ui --reflink=auto ${pkgs.OVMF.fd}/FV/OVMF.fd "$BIOS" - chmod a+w "$BIOS" - cp -ui --reflink=auto ${recover-efi}/nixos.img "$IMG" - chmod a+w "$IMG" - qemu-system-${platform} \ - -bios "$BIOS" \ - -drive file="$IMG",format=raw \ - -m 2G \ - $ARGS - ''; - runtimeInputs = with pkgs; [ tree rsync qemu ]; - }; - recover-vm = mk-recover-vm ""; - recover-kvm = mk-recover-vm "--enable-kvm"; + machines = import ./machines { inherit system pkgs lib; }; in { nixosConfigurations = { - inherit recover; + inherit (machines.recover) recover-os; }; packages.${system} = { - inherit recover-efi recover-vm recover-kvm; + inherit (machines.recover) recover-efi recover-vm recover-kvm; }; devShells.${system}.default = pkgs.mkShell { packages = - [ + with machines.recover; [ recover-vm recover-kvm ]; diff --git a/machines/default.nix b/machines/default.nix new file mode 100644 index 0000000..e8fb32d --- /dev/null +++ b/machines/default.nix @@ -0,0 +1,4 @@ +ctx: +{ + recover = import ./recover ctx; +} diff --git a/machines/recover/default.nix b/machines/recover/default.nix new file mode 100644 index 0000000..da21b1f --- /dev/null +++ b/machines/recover/default.nix @@ -0,0 +1,138 @@ +{ system, lib, pkgs, ... }: +let + inherit (lib) mkSystem mkRecoverVm; + inherit (pkgs.lib) mkDefault; + recover-module = { + nix = { + extraOptions = '' + experimental-features = nix-command flakes repl-flake + ''; + }; + nixpkgs.config.allowUnfree = true; + hardware.enableAllFirmware = true; + users = { + users.recover = { + password = "recover"; + isNormalUser = true; + home = "/home/recover"; + description = "Recover"; + extraGroups = [ "wheel" "networkmanager" ]; + }; + mutableUsers = false; + }; + networking = { + hostName = "recover-os"; + networkmanager.enable = true; + }; + boot = { + kernelParams = [ + "copytoram" + "console=ttyS0,115200" + "console=tty1" + "boot.shell_on_fail" + ]; + supportedFilesystems = [ + "btrfs" + "exfat" + "ext2" + "ext4" + "ntfs" + "vfat" + ]; + loader = { + timeout = 15; + grub = { + enable = true; + device = "nodev"; + efiSupport = true; + useOSProber = true; + memtest86.enable = true; + }; + }; + }; + fileSystems."/".device = mkDefault "none"; + + environment.systemPackages = with pkgs; [ + btop + coreutils + curl + ddrescue + efibootmgr + efivar + findutils + gnugrep + gnused + gnutar + gptfdisk + hdparm + inetutils + less + lsof + parted + pciutils + ripgrep + rsync + sdparm + smartmontools + sshfs + time + testdisk + unzip + usbutils + wget + which + zip + ntfs3g + f2fs-tools + jfsutils + nilfs-utils + reiserfsprogs + xfsprogs + xfsdump + gparted + ]; + + services = { + openssh.enable = true; + + xserver = { + enable = true; + windowManager.xmonad.enable = true; + displayManager = { + defaultSession = "none+xmonad"; + autoLogin = { + enable = true; + user = "recover"; + }; + }; + }; + }; + + programs = { + tmux.enable = true; + neovim = { + enable = true; + viAlias = true; + vimAlias = true; + }; + mtr.enable = true; + htop.enable = true; + }; + }; +in +rec { + recover-os = mkSystem { + inherit system; + modules = [ recover-module ]; + }; + + recover-efi = mkSystem { + inherit system; + modules = [ recover-module ]; + format = "raw-efi"; + }; + + recover-vm = mkRecoverVm recover-efi "recover" ""; + + recover-kvm = mkRecoverVm recover-efi "recover" "--enable-kvm"; +} diff --git a/recover.nix b/recover.nix deleted file mode 100644 index 2fef553..0000000 --- a/recover.nix +++ /dev/null @@ -1,117 +0,0 @@ -{ lib, pkgs, ... }: { - nix = { - extraOptions = '' - experimental-features = nix-command flakes repl-flake - ''; - }; - nixpkgs.config.allowUnfree = true; - hardware.enableAllFirmware = true; - users = { - users.recover = { - password = "recover"; - isNormalUser = true; - home = "/home/recover"; - description = "Recover"; - extraGroups = [ "wheel" "networkmanager" ]; - }; - mutableUsers = false; - }; - networking = { - hostName = "recover-os"; - networkmanager.enable = true; - }; - boot = { - kernelParams = [ - "copytoram" - "console=ttyS0,115200" - "console=tty1" - "boot.shell_on_fail" - ]; - supportedFilesystems = [ - "btrfs" - "exfat" - "ext2" - "ext4" - "ntfs" - "vfat" - ]; - loader = { - timeout = 15; - grub = { - enable = true; - device = "nodev"; - efiSupport = true; - useOSProber = true; - memtest86.enable = true; - }; - }; - }; - fileSystems."/".device = lib.mkDefault "none"; - - environment.systemPackages = with pkgs; [ - btop - coreutils - curl - ddrescue - efibootmgr - efivar - findutils - gnugrep - gnused - gnutar - gptfdisk - hdparm - inetutils - less - lsof - parted - pciutils - ripgrep - rsync - sdparm - smartmontools - sshfs - time - testdisk - unzip - usbutils - wget - which - zip - ntfs3g - f2fs-tools - jfsutils - nilfs-utils - reiserfsprogs - xfsprogs - xfsdump - gparted - ]; - - services = { - openssh.enable = true; - - xserver = { - enable = true; - windowManager.xmonad.enable = true; - displayManager = { - defaultSession = "none+xmonad"; - autoLogin = { - enable = true; - user = "recover"; - }; - }; - }; - }; - - programs = { - tmux.enable = true; - neovim = { - enable = true; - viAlias = true; - vimAlias = true; - }; - mtr.enable = true; - htop.enable = true; - }; -}