Update "Managing mutable files in NixOS" to v1.0.1

This commit is contained in:
Gabriel Arazas 2023-04-07 16:00:28 +08:00
parent 47b71558b7
commit 662b7c1e3b
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
5 changed files with 5 additions and 157 deletions

View File

@ -1,39 +0,0 @@
diff --git a/modules/home-manager/fetch-mutable-files.nix b/modules/home-manager/fetch-mutable-files.nix
new file mode 100644
index 0000000..9c66e05
--- /dev/null
+++ b/modules/home-manager/fetch-mutable-files.nix
@@ -0,0 +1,33 @@
+{ config, options, lib, pkgs, ... }:
+
+let
+ cfg = config.home.mutableFiles;
+ file = { config, name, ... }: {
+ };
+in
+{
+ options.home.mutableFile = lib.mkOption {
+ type = with lib.types; attrsOf (submodule file);
+ default = { };
+ description = lib.mkDoc ''
+ An attribute set of mutable files and directories to be fetched into the
+ home directory.
+ '';
+ example = lib.literalExpression ''
+ "''${config.xdg.userDirs.documents}/dotfiles" = {
+ url = "https://github.com/foo-dogsquared/dotfiles.git";
+ type = "git";
+ };
+
+ "''${config.xdg.userDirs.documents}/top-secret" = {
+ url = "https://example.com/file.zip";
+ type = "fetch";
+ };
+ '';
+ };
+
+ config = {
+ systemd.user.services.fetch-mutable-files = {
+ };
+ };
+}

View File

@ -1,54 +0,0 @@
diff --git a/modules/home-manager/fetch-mutable-files.nix b/modules/home-manager/fetch-mutable-files.nix
index 9c66e05..aa00cac 100644
--- a/modules/home-manager/fetch-mutable-files.nix
+++ b/modules/home-manager/fetch-mutable-files.nix
@@ -2,12 +2,47 @@
let
cfg = config.home.mutableFiles;
- file = { config, name, ... }: {
+ file = baseDir: { config, name, ... }: {
+ options = {
+ url = lib.mkOption {
+ type = lib.types.str;
+ description = lib.mkDoc ''
+ The URL of the file to be fetched.
+ '';
+ example = "https://github.com/foo-dogsquared/dotfiles.git";
+ };
+
+ path = lib.mkOption {
+ type = lib.types.str;
+ description = lib.mkDoc ''
+ Output path of the resource relative to ${baseDir}.
+ '';
+ default = name;
+ apply = p:
+ if lib.hasPrefix "/" p then p else "${baseDir}/${p}";
+ };
+
+ type = lib.mkOption {
+ type = lib.types.enum [ "git" "fetch" ];
+ description = lib.mkDoc ''
+ Type that configures the behavior for fetching the URL.
+
+ This accept only certain keywords.
+
+ - For `fetch`, the file will be fetched with `curl`.
+ - For `git`, it will be fetched with `git clone`.
+
+ The default type is `fetch`.
+ '';
+ default = "fetch";
+ example = "git";
+ };
+ };
};
in
{
options.home.mutableFile = lib.mkOption {
- type = with lib.types; attrsOf (submodule file);
+ type = with lib.types; attrsOf (submodule (file config.home.homeDirectory));
default = { };
description = lib.mkDoc ''
An attribute set of mutable files and directories to be fetched into the

View File

@ -1,30 +0,0 @@
diff --git a/modules/home-manager/fetch-mutable-files.nix b/modules/home-manager/fetch-mutable-files.nix
index aa00cac..3d75414 100644
--- a/modules/home-manager/fetch-mutable-files.nix
+++ b/modules/home-manager/fetch-mutable-files.nix
@@ -63,6 +63,25 @@ in
config = {
systemd.user.services.fetch-mutable-files = {
+ Unit = {
+ Description = "Fetch mutable files from home-manager";
+ After = [ "default.target" "network-online.target" ];
+ Wants = [ "network-online.target" ];
+ };
+
+ Service = {
+ # We'll assume this service will download lots of files. We want the
+ # temporary files to only last along with the service.
+ PrivateUsers = true;
+ PrivateTmp = true;
+
+ Type = "oneshot";
+ RemainAfterExit = true;
+ # TODO: Complete this
+ ExecStart = "";
+ };
+
+ Install.WantedBy = [ "default.target" ];
};
};
}

View File

@ -1,29 +0,0 @@
diff --git a/modules/home-manager/fetch-mutable-files.nix b/modules/home-manager/fetch-mutable-files.nix
index 3d75414..c3e349b 100644
--- a/modules/home-manager/fetch-mutable-files.nix
+++ b/modules/home-manager/fetch-mutable-files.nix
@@ -77,8 +77,22 @@ in
Type = "oneshot";
RemainAfterExit = true;
- # TODO: Complete this
- ExecStart = "";
+ ExecStart = let
+ curl = "${lib.getBin pkgs.curl}/bin/curl";
+ git = "${lib.getBin pkgs.curl}/bin/git";
+ fetchCmds = lib.mapAttrsToList (file: value:
+ let
+ inherit (value) type;
+ path = lib.escapeShellArg value.path;
+ url = lib.escapeURL value.url;
+ in ''
+ ${lib.optionalString (type == "git") "[ -d ${path} ] || ${git} clone ${url} ${path}"}
+ ${lib.optionalString (type == "fetch") "[ -d ${path} ] || ${curl} ${url} --output ${path}"}
+ '') cfg;
+ shellScript = pkgs.writeShellScript "fetch-mutable-files" ''
+ ${lib.concatStringsSep "\n" fetchCmds}
+ '';
+ in builtins.toString shellScript;
};
Install.WantedBy = [ "default.target" ];

View File

@ -8,7 +8,7 @@ tags:
= Managing mutable files in NixOS
Gabriel Arazas <foodogsquared@foodogsquared.one>
2023-03-24
v1.0.1, 2023-04-07: Update to use the Git blob include processor
:home-manager-rev: bb4b25b302dbf0f527f190461b080b5262871756
:nix-flakes-post: ../2023-03-05-combining-traditional-dotfiles-and-nixos-configurations-with-nix-flakes/index.adoc
@ -118,7 +118,7 @@ Let's first create the skeleton for the module.
.`modules/home-manager/fetch-mutable-files.nix`
[source, nix]
----
include::./assets/fetch-mutable-files-skeleton.nix[]
include::git:{doccontentref}~3[path=modules/home-manager/fetch-mutable-files.nix]
----
We have yet to define certain parts including what each attribute could contain.
@ -138,7 +138,7 @@ git apply file.patch
[source, patch]
----
include::./assets/patches/0002-fetch-mutable-files-add-file-submodule.patch[]
include::git:{doccontentref}~2[opts=diff]
----
Take note we also added the `path` attribute that comes with a function to handle the path.
@ -176,7 +176,7 @@ curl "https://example.org" || echo "ERROR"
[source, patch]
----
include::./assets/patches/0003-fetch-mutable-files-add-initial-systemd-service.patch[]
include::git:{doccontentref}~1[opts=diff]
----
Creating the shell script should be trivial.
@ -185,7 +185,7 @@ Here's one way to let Nix generate our shell script featuring link:https://nixos
[source, patch]
----
include::./assets/patches/0004-fetch-mutable-files-add-the-shell-script-for-the-ser.patch[]
include::git:{doccontentref}[opts=diff]
----
[NOTE]