{
  pkgs,
  lib,
  config,
  osConfig,
  inputs,
  ...
}: let
  cfg = osConfig.mods;
  installScript = pkgs.writeShellApplication {
    name = "install-flake";
    text = ''
      msg() {
        printf "\033[01;32minstall-flake: \033[0m%s\n" "$1"
      }

      msg "Cloning config..."
      if [[ ! "$PWD" == "$HOME" ]]; then
        cd
      fi
      if [[ ! -d "home" ]]; then
        git clone https://codeberg.org/muon/home
      else
        msg "Already cloned!"
      fi
      cd home
      nvim hosts/"$(hostname)"/disk-config.nix

      msg "Running disko..."
      if ! mount | grep "on /mnt" ; then
        sudo nix --experimental-features "nix-command flakes" run github:nix-community/disko/latest -- --mode destroy,format,mount hosts/"$(hostname)"/disk-config.nix
      else
        msg "Already mounted!"
      fi

      msg "Making blank root snapshot..."
      sudo mkdir -p /tmp/mnt
      sudo mount /dev/mapper/crypted /tmp/mnt
      if [[ ! -e /tmp/mnt/root-blank ]]; then
        sudo btrfs subvolume snapshot -r /tmp/mnt/root /tmp/mnt/root-blank
      else
        msg "Already snapshotted!"
      fi
      sudo umount /tmp/mnt

      msg "Retrieving sops key..."
      sudo mkdir -p /mnt/persist/var/lib/sops-nix
      if [[ ! -e /mnt/persist/var/lib/sops-nix/key.txt ]]; then
        rbw config set email admin@muon.host
        rbw config set base_url https://vault.muon.host
        rbw login
        rbw get sops | sudo tee /mnt/persist/var/lib/sops-nix/key.txt
        # sudo cp /etc/sops-nix/key.txt /mnt/persist/var/lib/sops-nix/key.txt
        sudo chown muon:users /mnt/persist/var/lib/sops-nix -R
      else
        msg "Already retrieved!"
      fi

      msg "Setting hardware config..."
      sudo nixos-generate-config --no-filesystems --root /mnt
      cp /mnt/etc/nixos/hardware-configuration.nix hosts/"$(hostname)"/hardware-configuration.nix

      msg "Installing NixOS config..."
      sudo nixos-install --root /mnt --no-root-passwd --flake .#"$(hostname)"

      msg "Edit persistent filesystem..."
      PERSIST="$HOME/persist.sh"
      if [[ ! -e $PERSIST ]]; then
        sudo cp "$HOME/persist.txt" "$PERSIST"
        sudo chmod +w "$PERSIST"
      fi
      nvim "$PERSIST"

      msg "Copying persistent filesystem..."
      make_persist() {
        F="$1"
        P="/mnt/persist''${1#/mnt}"
        echo "$P"
        if [[ -e "$F" ]]; then
          sudo mkdir -p "$(dirname "$P")"
        else
          msg "Error: $F does not exist!"
          return 0
        fi
        if [[ -d "$F" ]]; then
          if [[ -n "$( ls -A "$F")" ]]; then
            sudo cp -r "$F/"* "$P"
          fi
        elif [[ -f "$F" ]]; then
          sudo cp "$F" "$P"
        fi
      }
      grep -v '^#' "$PERSIST" | while read -r file ; do
        make_persist "$file"
      done
    '';
  };
in {
  # Install
  # sops.age.keyFile = lib.mkForce "/etc/sops-nix/key.txt";
  programs.atuin.enable = lib.mkForce false;
  home.file.persist = {
    target = "persist.txt";
    text = ''
      /mnt/var/lib/nixos
      /mnt/var/lib/systemd/coredump
      /mnt/etc/machine-id
      /etc/NetworkManager
      /var/lib/NetworkManager
    '';
  };
  home.packages = with pkgs;
    [
      installScript
    ]
    # Modules
    ++ [
      pulseaudio
      pavucontrol
      alsa-utils
    ];

  mods.xdg.enable = true;
  mods.i3.enable = true;
  mods.battery.enable = true;
  mods.terminal.zsh.enable = true;
  mods.terminal.emulator.enable = true;
  mods.terminal.development.enable = true;
  mods.terminal.tools.enable = true;
  mods.desktop.development.enable = true;
  mods.desktop.productivity.enable = false;

  # Hardware preferences
  ## Monitors
  services.autorandr.enable = true;
  programs.autorandr = {
    enable = true;
    hooks.postswitch = {
      "notify-i3" = "${pkgs.i3}/bin/i3-msg restart";
      "set-wallpaper" = ''
        ${lib.getExe pkgs.feh} --bg-fill --nofehbg ${./wallpaper.png}
      '';
    };
  };

  # Version of first install
  home.stateVersion = "23.05";
}
