From c3f715d01024da20a91565a3a256451580ef2ee2 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 11 Apr 2025 17:45:47 -0400 Subject: [PATCH] Add the install_file module from the steam deck config. --- nix/configuration/configuration.nix | 1 + .../util/install_files/default.nix | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 nix/configuration/util/install_files/default.nix diff --git a/nix/configuration/configuration.nix b/nix/configuration/configuration.nix index bf55f68..35e5d7c 100644 --- a/nix/configuration/configuration.nix +++ b/nix/configuration/configuration.nix @@ -74,6 +74,7 @@ ./roles/zfs ./roles/zrepl ./roles/zsh + ./util/install_files ./util/unfree_polyfill ]; diff --git a/nix/configuration/util/install_files/default.nix b/nix/configuration/util/install_files/default.nix new file mode 100644 index 0000000..ce20bf4 --- /dev/null +++ b/nix/configuration/util/install_files/default.nix @@ -0,0 +1,101 @@ +{ + config, + lib, + pkgs, + home-manager, + ... +}: + +let + inherit (lib) + filter + attrNames + ; +in +{ + imports = [ ]; + + options.me = { + install.file = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule ( + { name, config, ... }: + { + options = { + enable = lib.mkOption { + type = lib.types.bool; + default = true; + defaultText = "me.install.file.‹name›.enable"; + example = false; + description = "Whether we want to install this file."; + }; + mode = lib.mkOption { + type = lib.types.str; + default = "0444"; + defaultText = "me.install.file.‹name›.mode"; + example = "0750"; + description = "The read, write, execute permission flags."; + }; + source = lib.mkOption { + type = lib.types.path; + defaultText = "me.install.file.‹name›.source"; + example = ./files/foo.txt; + description = "The source file to install into the destination."; + }; + target = lib.mkOption { + type = lib.types.str; + defaultText = "me.install.file.‹name›.target"; + example = ".local/share/foo/bar.txt"; + description = "The path where the file should be written."; + }; + }; + + config = { + target = lib.mkDefault name; + }; + } + ) + ); + defaultText = "me.install.file.‹name›"; + default = { }; + example = lib.literalExpression '' + { + ".config/foo/bar.txt" = { + source = ./files/bar.txt + }; + } + ''; + }; + }; + + config = lib.mkMerge [ + (lib.mkIf (config.me.install.file != { }) ( + let + cfg = config.me.install.file; + install_file_targets = filter (target: config.me.install.file."${target}".enable) (attrNames cfg); + in + { + home.activation = { + installFiles = home-manager.lib.hm.dag.entryAfter [ "writeBoundary" ] ( + let + install_commands = builtins.map ( + target: + let + target_config = config.me.install.file."${target}"; + source = lib.strings.escapeShellArg "${target_config.source}"; + destination = lib.strings.escapeShellArg target; + mode = lib.strings.escapeShellArg "${target_config.mode}"; + in + # $DRY_RUN_CMD ${pkgs.toyboy}/bin/install $VERBOSE_ARG -D -m ${mode} ${source} ${destination} + '' + $DRY_RUN_CMD install $VERBOSE_ARG -D --compare -m ${mode} ${source} ${destination} + '' + ) install_file_targets; + in + (lib.strings.concatStringsSep "\n" install_commands) + ); + }; + } + )) + ]; +}