different-name / nix-files

My NixOS configuration files
GNU General Public License v3.0
3 stars 0 forks source link

fd alias to list stray files in /persist #21

Closed different-name closed 2 months ago

different-name commented 2 months ago

Files that are removed from persistence remain in /persist, a way to combat this would be to add a post activation script that goes through each directory in /persist, excluding those persisted and displays them as a warning, so that they can be removed or added back to the persistence configuration

different-name commented 2 months ago

I made a post activation script for the impermanence NixOS module, but I realised when I started working on the home-manager module won't actually be able to see a log from the home-manager module when rebuilding my system, which is arguably more important to me.

I'm going to pivot and adapt a version of the fd alias I use for detecting files I might want to persist

That way I can just run a command and detect what is in /persist that I probably don't want anymore

Not as clean as I would have hoped, but I can't see I way I would have the home-manager module output to the rebuild logs

Here's a patch for https://github.com/nix-community/impermanence/commit/23c1f06316b67cb5dabdfe2973da3785cfe9c34a with my changes in case I decide to come back to this at some point

--- a/nixos.nix
+++ b/nixos.nix
@@ -658,6 +658,18 @@ in
             ${concatMapStrings mkPersistFile files}
             exit $_status
           '';
+
+        listStrayFilesScript = let
+          searchPaths = filter (n: cfg.${n}.enable) (attrNames cfg);
+          searchString = concatStringsSep " " (map escapeShellArg searchPaths);
+          excludeFiles = map (file: concatPaths [file.persistentStoragePath file.filePath]) files;
+          excludeDirs = map (dir: concatPaths [dir.persistentStoragePath dir.dirPath]) directories;
+          excludeString = concatStringsSep " -o " (map (path: "-path " + escapeShellArg path) (excludeFiles ++ excludeDirs));
+        in
+          pkgs.writeShellScript "impermanence-list-stray-files" ''
+            echo -e "\033[1;33mimpermanence: The following files/directories were found in your persistent storage were not found in your persistence configuration\033[0m"
+            find ${searchString} \( ${excludeString} \) -prune -o \( -empty -type d -print0 \) -o \( -type f -print0 \) | xargs -0 -I{} /bin/sh -c 'echo -e "\033[1;33m{}\033[0m"'
+          '';
       in
       {
         "createPersistentStorageDirs" = {
@@ -668,6 +680,9 @@ in
           deps = [ "createPersistentStorageDirs" ];
           text = "${persistFileScript}";
         };
+        "listStrayFiles" = {
+          text = "${listStrayFilesScript}";
+        };
       };

     assertions =
different-name commented 2 months ago

I was not able to adapt my current fd alias to work, fd does not support excluding absolute paths unfortunately (see https://github.com/sharkdp/fd/issues/851), and I could not think of a workaround

I was able to implement the same functionality using the find command, and reworked the alias using fd to use find aswell

Implemented using find in https://github.com/Different-Name/nix-files/commit/374793abe045fb382dc503f4a5bfc1eec4bb5ec1