From 78f154b38ae474f1efe457fa129feaef7bdca73c Mon Sep 17 00:00:00 2001
From: Matei Adriel <rafaeladriel11@gmail.com>
Date: Thu, 27 Apr 2023 02:08:20 +0300
Subject: [PATCH] Set up the basics of imperanence

---
 flake.lock                                    |   6 +-
 home/adrielus/features/cli/fish/default.nix   |  11 +-
 .../features/desktop/common/discord.nix       |   5 +-
 .../features/desktop/common/firefox.nix       | 209 ++++++++++--------
 .../features/desktop/common/games/default.nix |   3 -
 .../features/desktop/common/games/lutris.nix  |  12 -
 .../features/desktop/common/qbittorrent.nix   |  10 +-
 .../features/desktop/common/signal.nix        |   9 +
 home/adrielus/features/games/default.nix      |   3 +
 home/adrielus/features/games/lutris.nix       |  14 ++
 home/adrielus/features/games/steam.nix        |  20 ++
 .../{desktop/common => }/games/wine.nix       |   0
 home/adrielus/global/default.nix              |   8 +-
 home/adrielus/tethys.nix                      |   7 +-
 hosts/nixos/common/global/default.nix         |   8 +-
 hosts/nixos/common/global/openssh.nix         |  11 +-
 .../nixos/common/global/wireless/default.nix  |   9 +-
 hosts/nixos/common/optional/touchpad.nix      |   2 +-
 hosts/nixos/common/optional/xdg-portal.nix    |  10 +
 hosts/nixos/tethys/default.nix                |   1 +
 20 files changed, 224 insertions(+), 134 deletions(-)
 delete mode 100644 home/adrielus/features/desktop/common/games/default.nix
 delete mode 100644 home/adrielus/features/desktop/common/games/lutris.nix
 create mode 100644 home/adrielus/features/desktop/common/signal.nix
 create mode 100644 home/adrielus/features/games/default.nix
 create mode 100644 home/adrielus/features/games/lutris.nix
 create mode 100644 home/adrielus/features/games/steam.nix
 rename home/adrielus/features/{desktop/common => }/games/wine.nix (100%)
 create mode 100644 hosts/nixos/common/optional/xdg-portal.nix

diff --git a/flake.lock b/flake.lock
index fbdcffc..3ea04c9 100644
--- a/flake.lock
+++ b/flake.lock
@@ -310,11 +310,11 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1677779205,
-        "narHash": "sha256-6DBjL9wjq86p2GczmwnHtFRnWPBPItc67gapWENBgX8=",
+        "lastModified": 1682493015,
+        "narHash": "sha256-KiMOAe8u83QDF40Z25kWMkZbqr9U+mvjvEJy5Qvw0nw=",
         "owner": "nixos",
         "repo": "nixpkgs",
-        "rev": "96e18717904dfedcd884541e5a92bf9ff632cf39",
+        "rev": "60c0f762658916a4a5b5a36b3e06486f8301daf4",
         "type": "github"
       },
       "original": {
diff --git a/home/adrielus/features/cli/fish/default.nix b/home/adrielus/features/cli/fish/default.nix
index 7698c5c..536b1d6 100644
--- a/home/adrielus/features/cli/fish/default.nix
+++ b/home/adrielus/features/cli/fish/default.nix
@@ -5,10 +5,17 @@
 
     shellAbbrs = {
       battery = "acpi";
-      cat = "bat";
     };
 
-    plugins = with pkgs.fishPlugins; [
+    shellAliases = {
+      cat = "bat";
+      df = "df -h";
+      du = "du -h";
+      duh = "du -hd 1"; # short for du here
+    };
+
+    # with pkgs.fishPlugins;
+    plugins = [
       # Jump to directories by typing "z <directory-name>"
       {
         name = "z";
diff --git a/home/adrielus/features/desktop/common/discord.nix b/home/adrielus/features/desktop/common/discord.nix
index ebf1935..43ec9a7 100644
--- a/home/adrielus/features/desktop/common/discord.nix
+++ b/home/adrielus/features/desktop/common/discord.nix
@@ -1,8 +1,11 @@
-# TODO(imperanence): handle persistence
 {
   programs.discord = {
     enable = true;
     disableUpdateCheck = true;
     enableDevtools = true;
   };
+
+  home.persistence."/persist/home/adrielus".directories = [
+    ".config/discord" # Why tf does discord store it's state here 💀
+  ];
 }
diff --git a/home/adrielus/features/desktop/common/firefox.nix b/home/adrielus/features/desktop/common/firefox.nix
index 078d359..b3be627 100644
--- a/home/adrielus/features/desktop/common/firefox.nix
+++ b/home/adrielus/features/desktop/common/firefox.nix
@@ -1,45 +1,22 @@
-# TODO(imperanence): handle persistence
 { pkgs, inputs, ... }:
-let
-  mkBasicSearchEngine = { aliases, url, param }: {
-    urls = [{
-      template = url;
-      params = [
-        { name = param; value = "{searchTerms}"; }
-      ];
-    }];
-
-    definedAliases = aliases;
-  };
-
-  mkNixPackagesEngine = { aliases, type }:
-    let basicEngine = mkBasicSearchEngine
-      {
-        aliases = aliases;
-        url = "https://search.nixos.org/${type}";
-        param = "query";
-      };
-    in
-    basicEngine // {
-      icon = "${pkgs.nixos-icons}/share/icons/hicolor/scalable/apps/nix-snowflake.svg";
-    };
-in
 {
   programs.firefox = {
     enable = true;
     profiles.adrielus = {
+      # {{{ High level user settings
       # Unique user id
       id = 0;
 
       # Make this the default user
       isDefault = true;
 
-      # Forcefully replace the search configuration 
+      # Forcefully replace the search configuration
       search.force = true;
 
       # Set default search engine
       search.default = "Google";
-
+      # }}}
+      # {{{ Extensions
       extensions = with inputs.firefox-addons.packages.${pkgs.system}; [
         buster-captcha-solver
         bypass-paywalls-clean
@@ -67,77 +44,109 @@ in
         vimium-c # vim keybinds
         youtube-shorts-block
       ];
+      # }}}
+      # {{{ Search engines
+      search.engines =
+        let
+          # {{{ Search engine creation helpers
+          mkBasicSearchEngine = { aliases, url, param }: {
+            urls = [{
+              template = url;
+              params = [
+                { name = param; value = "{searchTerms}"; }
+              ];
+            }];
 
-      # Specify custom search engines
-      search.engines = {
-        "Nix Packages" = mkNixPackagesEngine {
-          aliases = [ "@np" "@nix-packages" ];
-          type = "packages";
+            definedAliases = aliases;
+          };
+
+          mkNixPackagesEngine = { aliases, type }:
+            let basicEngine = mkBasicSearchEngine
+              {
+                aliases = aliases;
+                url = "https://search.nixos.org/${type}";
+                param = "query";
+              };
+            in
+            basicEngine // {
+              icon = "${pkgs.nixos-icons}/share/icons/hicolor/scalable/apps/nix-snowflake.svg";
+            };
+          # }}}
+        in
+        # {{{ Engine declarations
+        {
+          "Nix Packages" = mkNixPackagesEngine {
+            aliases = [ "@np" "@nix-packages" ];
+            type = "packages";
+          };
+
+          "Nix options" = mkNixPackagesEngine {
+            aliases = [ "@no" "@nix-options" ];
+            type = "options";
+          };
+
+          # Purescript packages
+          "Pursuit" = mkBasicSearchEngine {
+            url = "https://pursuit.purescript.org/search";
+            param = "q";
+            aliases = [ "@ps" "@pursuit" ];
+          };
+
+          "Wikipedia" = mkBasicSearchEngine {
+            url = "https://en.wikipedia.org/wiki/Special:Search";
+            param = "search";
+            aliases = [ "@wk" "@wikipedia" ];
+          };
+
+          "Github" = mkBasicSearchEngine {
+            url = "https://github.com/search";
+            param = "q";
+            aliases = [ "@gh" "@github" ];
+          };
+
+          "Youtube" = mkBasicSearchEngine {
+            url = "https://www.youtube.com/results";
+            param = "search_query";
+            aliases = [ "@yt" "@youtube" ];
+          };
+
+          "Noita wiki" = mkBasicSearchEngine {
+            url = "https://noita.wiki.gg/index.php";
+            param = "search";
+            aliases = [ "@noita" ];
+          };
+
+          "Rain world wiki" = mkBasicSearchEngine {
+            url = "https://rainworld.miraheze.org/w/index.php";
+            param = "search";
+            aliases = [ "@rw" "@rain-world" ];
+          };
+
+          "Factorio wiki" = mkBasicSearchEngine {
+            url = "https://wiki.factorio.com/index.php";
+            param = "search";
+            aliases = [ "@fw" "@factorio-wiki" ];
+          };
+
+          "Factorio mod portal" = mkBasicSearchEngine {
+            url = "https://mods.factorio.com/";
+            param = "query";
+            aliases = [ "@fm" "@factorio-mods" ];
+          };
+
+          "Google".metaData.alias = "@g";
         };
-
-        "Nix options" = mkNixPackagesEngine {
-          aliases = [ "@no" "@nix-options" ];
-          type = "options";
-        };
-
-        # Purescript packages
-        "Pursuit" = mkBasicSearchEngine {
-          url = "https://pursuit.purescript.org/search";
-          param = "q";
-          aliases = [ "@ps" "@pursuit" ];
-        };
-
-        "Wikipedia" = mkBasicSearchEngine {
-          url = "https://en.wikipedia.org/wiki/Special:Search";
-          param = "search";
-          aliases = [ "@wk" "@wikipedia" ];
-        };
-
-        "Github" = mkBasicSearchEngine {
-          url = "https://github.com/search";
-          param = "q";
-          aliases = [ "@gh" "@github" ];
-        };
-
-        "Youtube" = mkBasicSearchEngine {
-          url = "https://www.youtube.com/results";
-          param = "search_query";
-          aliases = [ "@yt" "@youtube" ];
-        };
-
-        "Noita wiki" = mkBasicSearchEngine {
-          url = "https://noita.wiki.gg/index.php";
-          param = "search";
-          aliases = [ "@noita" ];
-        };
-
-        "Rain world wiki" = mkBasicSearchEngine {
-          url = "https://rainworld.miraheze.org/w/index.php";
-          param = "search";
-          aliases = [ "@rw" "@rain-world" ];
-        };
-
-        "Factorio wiki" = mkBasicSearchEngine {
-          url = "https://wiki.factorio.com/index.php";
-          param = "search";
-          aliases = [ "@fw" "@factorio-wiki" ];
-        };
-
-        "Factorio mod portal" = mkBasicSearchEngine {
-          url = "https://mods.factorio.com/";
-          param = "query";
-          aliases = [ "@fm" "@factorio-mods" ];
-        };
-
-        "Google".metaData.alias = "@g";
-      };
-
+      # }}}
+      # }}}
+      # {{{ Other lower level settings
       settings = {
         # Required for figma to be able to export to svg
         "dom.events.asyncClipboard.clipboardItem" = true;
       };
+      # }}}
     };
 
+    # {{{ Standalone "apps" which actually run inside a browser.
     apps = {
       # {{{ Job stuff
       asana = {
@@ -170,5 +179,25 @@ in
         id = 4;
       };
     };
+    # }}}
   };
+
+  # {{{ Make firefox the default
+  # Use firefox as the default browser to open stuff.
+  # xdg.mimeApps.defaultApplications = {
+  #   "text/html" = [ "firefox.desktop" ];
+  #   "text/xml" = [ "firefox.desktop" ];
+  #   "x-scheme-handler/http" = [ "firefox.desktop" ];
+  #   "x-scheme-handler/https" = [ "firefox.desktop" ];
+  # };
+
+  # Tell apps firefox is the default browser using an env var.
+  home.sessionVariables.BROWSER = "firefox";
+  # }}}
+  # {{{ Persistence
+  home.persistence."/persist/home/adrielus".directories = [
+    ".cache/mozilla/firefox" # Non important cache
+    ".mozilla/firefox" # More important stuff
+  ];
+  # }}}
 }
diff --git a/home/adrielus/features/desktop/common/games/default.nix b/home/adrielus/features/desktop/common/games/default.nix
deleted file mode 100644
index 67b6f23..0000000
--- a/home/adrielus/features/desktop/common/games/default.nix
+++ /dev/null
@@ -1,3 +0,0 @@
-{
-  imports = [ ./wine.nix ./lutris.nix ];
-}
diff --git a/home/adrielus/features/desktop/common/games/lutris.nix b/home/adrielus/features/desktop/common/games/lutris.nix
deleted file mode 100644
index 5a8ee8f..0000000
--- a/home/adrielus/features/desktop/common/games/lutris.nix
+++ /dev/null
@@ -1,12 +0,0 @@
-{ pkgs, config, ... }: {
-  home.packages = [
-    pkgs.lutris
-  ];
-
-  # home.persistence."/persist".directories = [
-  #   "${config.xdg.configHome}/lutris" # General config data
-  #   "${config.xdg.cacheHome}/banners" # Game banners
-  #   "${config.xdg.cacheHome}/coverart" # Game cover art
-  #   "${config.home.homeDirectory}/Games" # Game directory
-  # ];
-}
diff --git a/home/adrielus/features/desktop/common/qbittorrent.nix b/home/adrielus/features/desktop/common/qbittorrent.nix
index a8d0fa7..8521dc9 100644
--- a/home/adrielus/features/desktop/common/qbittorrent.nix
+++ b/home/adrielus/features/desktop/common/qbittorrent.nix
@@ -3,9 +3,9 @@
     pkgs.qbittorrent
   ];
 
-  # home.persistence."/persist".directories = [
-  #   "${config.xdg.configHome}/qBittorrent" # Config options
-  #   # TODO: investigate which subdirectories/files I actually want to keep
-  #   "${config.xdg.dataHome}/qBittorrent" # Torrent files, logs, etc 
-  # ];
+  home.persistence."/persist/home/adrielus".directories = [
+    ".config/qBittorrent" # Config options
+    # TODO: investigate which subdirectories/files I actually want to keep
+    ".local/share/qBittorrent" # Torrent files, logs, etc 
+  ];
 }
diff --git a/home/adrielus/features/desktop/common/signal.nix b/home/adrielus/features/desktop/common/signal.nix
new file mode 100644
index 0000000..2429e80
--- /dev/null
+++ b/home/adrielus/features/desktop/common/signal.nix
@@ -0,0 +1,9 @@
+{ pkgs, ... }: {
+  home.packages = [
+    pkgs.signal-desktop # Signal client
+  ];
+
+  home.persistence."/persist/home/adrielus".directories = [
+    ".config/Signal" # Why tf does signal store it's state here 💀
+  ];
+}
diff --git a/home/adrielus/features/games/default.nix b/home/adrielus/features/games/default.nix
new file mode 100644
index 0000000..d66e834
--- /dev/null
+++ b/home/adrielus/features/games/default.nix
@@ -0,0 +1,3 @@
+{
+  imports = [ ./wine.nix ./lutris.nix ./steam.nix ];
+}
diff --git a/home/adrielus/features/games/lutris.nix b/home/adrielus/features/games/lutris.nix
new file mode 100644
index 0000000..ed36a2e
--- /dev/null
+++ b/home/adrielus/features/games/lutris.nix
@@ -0,0 +1,14 @@
+{ pkgs, config, ... }: {
+  home.packages = [
+    pkgs.lutris
+  ];
+
+  home.persistence."/persist/home/adrielus".directories = [
+    ".config/lutris" # General config data
+    ".cache/lutris/banners" # Game banners
+    ".cache/lutris/coverart" # Game cover art
+
+    # Aparently IO intensive stuff like games prefer symlinks?
+    { directory = "Games/Lutris"; method = "symlink"; } # Lutris games
+  ];
+}
diff --git a/home/adrielus/features/games/steam.nix b/home/adrielus/features/games/steam.nix
new file mode 100644
index 0000000..b7f9560
--- /dev/null
+++ b/home/adrielus/features/games/steam.nix
@@ -0,0 +1,20 @@
+# Although steam is installed globally by nixos,
+# there's some extra settings we make for a specific user!
+{
+  home.persistence."/persist/home/adrielus" = {
+    files = [
+      ".steam/registry.vdf" # It seems like auto-login does not work without this
+    ];
+
+    directories = [
+      # TODO: perhaps this should leave in it's own file?
+      ".factorio"
+
+      # A couple of games don't play well with bindfs
+      {
+        directory = ".local/share/Steam";
+        method = "symlink";
+      }
+    ];
+  };
+}
diff --git a/home/adrielus/features/desktop/common/games/wine.nix b/home/adrielus/features/games/wine.nix
similarity index 100%
rename from home/adrielus/features/desktop/common/games/wine.nix
rename to home/adrielus/features/games/wine.nix
diff --git a/home/adrielus/global/default.nix b/home/adrielus/global/default.nix
index bdf1778..2ed2b91 100644
--- a/home/adrielus/global/default.nix
+++ b/home/adrielus/global/default.nix
@@ -5,7 +5,7 @@ let
     inputs.stylix.homeManagerModules.stylix
     inputs.homeage.homeManagerModules.homeage
     inputs.nur.nixosModules.nur
-    # inputs.impermanence.nixosModules.home-manager.impermanence
+    inputs.impermanence.nixosModules.home-manager.impermanence
 
     ../features/cli
     ../features/neovim
@@ -51,4 +51,10 @@ in
 
   # Where homeage should look for our ssh key
   homeage.identityPaths = [ "~/.ssh/id_ed25519" ];
+
+  # Allow root to read persistent files from this user.
+  home.persistence."/persist/home/adrielus".allowOther = true;
+
+  # Set the xdg env vars
+  xdg.enable = true;
 }
diff --git a/home/adrielus/tethys.nix b/home/adrielus/tethys.nix
index b65f18d..0059745 100644
--- a/home/adrielus/tethys.nix
+++ b/home/adrielus/tethys.nix
@@ -3,17 +3,18 @@
     ./global
     ./features/desktop/xmonad.nix
     ./features/desktop/common/discord.nix
+    ./features/desktop/common/signal.nix
     ./features/desktop/common/qbittorrent.nix
     ./features/desktop/common/zathura.nix
     ./features/desktop/common/firefox.nix
-    ./features/desktop/common/games
+    ./features/games
   ];
 
   # Arbitrary extra packages
   home.packages = with pkgs; [
     # Desktop apps
-    signal-desktop # Signal client
     zoom-us # Zoom client 🤮
+    element-desktop # Matrix client
     obsidian # Notes
     peek # GIF recorder
     vlc # Video player
@@ -21,7 +22,7 @@
     libreoffice # Free office suite
     lmms # Music software
     kicad # PCB editing
-    google-chrome # Not my primary browser, but sometimes needed in webdev
+    # google-chrome # Not my primary browser, but sometimes needed in webdev
     # obs-studio # video recorder
 
     # Clis
diff --git a/hosts/nixos/common/global/default.nix b/hosts/nixos/common/global/default.nix
index 97dcb08..8226cdd 100644
--- a/hosts/nixos/common/global/default.nix
+++ b/hosts/nixos/common/global/default.nix
@@ -6,7 +6,7 @@ let
     inputs.stylix.nixosModules.stylix
     inputs.slambda.nixosModule
     inputs.nur.nixosModules.nur
-    # inputs.impermanence.nixosModule
+    inputs.impermanence.nixosModule
 
     ./nix.nix
     ./openssh.nix
@@ -20,9 +20,9 @@ in
   # Import all modules defined in modules/nixos
   imports = builtins.attrValues outputs.nixosModules ++ imports;
 
-  age.identityPaths = [
-    "/etc/ssh/ssh_host_ed25519_key"
-  ];
+  # Allow non root users to specify the "allowOther" option.
+  # See [the imperanence readme](https://github.com/nix-community/impermanence#home-manager)
+  programs.fuse.userAllowOther = true;
 
   nixpkgs = {
     # Add all overlays defined in the overlays directory
diff --git a/hosts/nixos/common/global/openssh.nix b/hosts/nixos/common/global/openssh.nix
index 03908af..23ecc10 100644
--- a/hosts/nixos/common/global/openssh.nix
+++ b/hosts/nixos/common/global/openssh.nix
@@ -30,10 +30,13 @@ in
     gatewayPorts = "clientspecified";
 
     # Generate ssh key
-    hostKeys = [{
-      path = "/persist/etc/ssh/ssh_host_ed25519_key";
-      type = "ed25519";
-    }];
+    hostKeys =
+      let mkKey = type: path: extra: { inherit type path; } // extra;
+      in
+      [
+        (mkKey "ed25519" "/persist/etc/ssh/ssh_host_ed25519_key" { })
+        (mkKey "rsa" "/persist/etc/ssh/ssh_host_rsa_key" { bits = 4096; })
+      ];
   };
 
   # Passwordless sudo when SSH'ing with keys
diff --git a/hosts/nixos/common/global/wireless/default.nix b/hosts/nixos/common/global/wireless/default.nix
index 7506842..cb86f89 100644
--- a/hosts/nixos/common/global/wireless/default.nix
+++ b/hosts/nixos/common/global/wireless/default.nix
@@ -43,10 +43,9 @@
   # Ensure group exists
   users.groups.network = { };
 
+  # TODO: figure out why this does not work!
   # Persist imperative config
-  # environment.persistence = {
-  #   "/persist".files = [
-  #     "/etc/wpa_supplicant.conf"
-  #   ];
-  # };
+  # environment.persistence."/persist".files = [
+  #   "/etc/wpa_supplicant.conf"
+  # ];
 }
diff --git a/hosts/nixos/common/optional/touchpad.nix b/hosts/nixos/common/optional/touchpad.nix
index b5e119f..f39f5a5 100644
--- a/hosts/nixos/common/optional/touchpad.nix
+++ b/hosts/nixos/common/optional/touchpad.nix
@@ -7,7 +7,7 @@
       # How fast we should scroll I think
       accelSpeed = "3.5";
 
-      # #TODO: I forgor what this did
+      # TODO: I forgor what this did
       naturalScrolling = true;
 
       # Dsiable the touchpad while typing 
diff --git a/hosts/nixos/common/optional/xdg-portal.nix b/hosts/nixos/common/optional/xdg-portal.nix
new file mode 100644
index 0000000..4187ad1
--- /dev/null
+++ b/hosts/nixos/common/optional/xdg-portal.nix
@@ -0,0 +1,10 @@
+{ pkgs, lib, ... }: {
+  xdg.portal = {
+    enable = true;
+    xdgOpenUsePortal = true;
+    extraPortals = [ pkgs.xdg-desktop-portal ];
+  };
+
+  # HACK: copied from @lily on discord.
+  systemd.user.services.xdg-desktop-portal.path = lib.mkAfter [ "/run/current-system/sw" ];
+}
diff --git a/hosts/nixos/tethys/default.nix b/hosts/nixos/tethys/default.nix
index cb142cc..7152430 100644
--- a/hosts/nixos/tethys/default.nix
+++ b/hosts/nixos/tethys/default.nix
@@ -9,6 +9,7 @@
     ../common/optional/lightdm.nix
     ../common/optional/steam.nix
     ../common/optional/slambda.nix
+    ../common/optional/xdg-portal.nix
     ../common/optional/xmonad
 
     ./hardware-configuration.nix