{ config
, lib
, pkgs
, ...
}:

with lib;
let
  nom-system = pkgs.writeShellApplication {
    name = "nom-system";
    runtimeInputs = with pkgs; [ nix-output-monitor ];
    text = ''
      nom build --no-link "/home/moritz/.dotfiles#nixosConfigurations.$(hostname).config.system.build.toplevel"
    '';
  };
  nom-system-command = command: "${nom-system}/bin/nom-system && ${command}";
in
{
  users.users.moritz = {
    isNormalUser = true;
    home = "/home/moritz";
    extraGroups = [ "wheel" "networkmanager" "video" ];
    initialPassword = "password"; # CHANGE ME PLEASE
    openssh.authorizedKeys.keys = [
      "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGoAqa2m7hIzZ2LS96Z+RCIlRvhBM/j7h27tMBCwMT+a" # Moritz
      "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDhtwHDGAZshiQWKkCcPWV9tC83b+bKBgjDcjP/N2CKO" # Laptop
      "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKl8gMhwSf1NsP5gp14xbbyjqQLZzcHLb/XKRMoHdXgI" # Desktop
      "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDHlemuKagHwz2T5rEwgJNlVUdUdOXyPtCEzD73CrwY2zmpR4AMj7y9u3Rm7HwHUDjLap1ZFwg+53bAsVP6HFZccCXoIfO/8BL0WDGQJrfgb+A+UiRhSqSvyZ77bGJkadbBkadguz3qR3PHcb41DOlhuqVcHxsY8ceHMxAuyb0pLJVJLeytMD+CHS/r7hoj2hckTNAZ+VhCXBtdZfZ7uPUBxLfluYRNNMmdwCglsg3RUS242nJUzy3A84+CXIGeWmNG9Fu45IDkwMthxSW9klyU9R38R9DBDcugkyb6vz+JKSuRVAa47qh/kmtsYekfL3ul9D2JN32P8S+6ZoXx+gXupGJ0ltwJWAFkhLJ+yeXj9kCOv/mIUmCB14jMGsvKiSwV25O/twyjqe2LEkMVgimgrjEYoHu+ZTyp0iFtUvSrFo4tsAhfWPV9yj4F/hUksW7xKIwq5Niyx7he5M/XddudtnAximyiBDGCdJm1Ejl0UaGa6ZQv7y6VZdx0PyZuraT7l9ub8so6JlE4cVgSSU9vE0IS2QqBuHhsIjh8RVksoTR2NQbeDdGaGpGnq2C8y0rDXwE/EJA4LK45khX/GPn73n8F0kBG8dBrWgRDAEODpmebScO7d5mCeM0z3lPcRmh+3e3DPnVVOl+uR7udlc7NauLzl7q913UtxZaF1PlD7Q==" # GPG
    ];
  };

  time.timeZone = "Europe/Berlin";

  my = {
    nix = {
      gc.enable = true;
      optimise.enable = true;
    };
    bin.enable = true;
    shell = {
      abbreviations = {
        us = "systemctl --user";
        rs = "sudo systemctl";
      };
      aliases = {
        ls = "exa -lh --icons --git";
        cat = "bat";
        rm = "rm -i";
        mv = "mv -i";
        cd = "z";

        nixos-switch = nom-system-command "sudo nixos-rebuild switch --flake ~/.dotfiles";
        nixos-boot = nom-system-command "sudo nixos-rebuild boot --flake ~/.dotfiles";
        nixos-update = "pushd ~/.dotfiles && nix flake update && popd";

        latexwatch = ''find -type f -name "*.tex" | entr -c latexmk -pdf -silent'';
      };
      variables = { EDITOR = "vim"; };
    };
    programs = {
      direnv.enable = true;
      fish.enable = true;
      git.enable = true;
      gpg.enable = true;
    };
  };

  console.keyMap = "de";

  environment.systemPackages = with pkgs; [
    # archives
    p7zip
    unzip
    zip

    # file management
    trash-cli

    # monitoring
    htop
    bottom

    # nix
    comma
    nix-index
    nixpkgs-fmt
    statix
    manix
    nix-output-monitor
    attic

    # other
    bat
    cht-sh
    du-dust
    duf
    entr
    exa
    gparted
    neofetch
    ripgrep
    tmux
    up
    viu
    wget
    vim
  ];

  fonts.fonts = with pkgs; [
    (nerdfonts.override {
      fonts = [ "FiraCode" ];
    })
  ];

  programs = {
    mtr.enable = true;
    command-not-found.enable = true;
    starship.enable = true;
  };

  services = {
    sshd.enable = true; # for agenix
    btrfs.autoScrub.enable =
      lib.mkDefault
        (builtins.any (filesystem: filesystem.fsType == "btrfs")
          (builtins.attrValues config.fileSystems));
  };

  i18n.extraLocaleSettings = {
    # LC_NUMERIC = "de_DE.UTF-8";
    LC_PAPER = "de_DE.UTF-8";
    LC_TIME = "de_DE.UTF-8";
  };

  home-manager.users.moritz = {
    programs = {
      # Let Home Manager install and manage itself.
      home-manager.enable = true;
      fzf = {
        enable = true;
        defaultOptions = [
          "--height 50%"
        ];
      };
      zoxide.enable = true;
      navi.enable = true;
    };
    home = {
      username = "moritz";
      homeDirectory = "/home/moritz";
      stateVersion = "21.05";
    };
    xdg.userDirs.enable = true;
  };

  system.activationScripts.diff = ''
    if [ -e /run/current-system ]; then
      ${pkgs.nvd}/bin/nvd --nix-bin-dir ${pkgs.nix}/bin diff /run/current-system/ "$systemConfig"
    fi
  '';
}
