Skip to content

Commit 7facb3f

Browse files
committed
xdg.userDirs: improvements
- Don’t require Linux – everything here works fine on Darwin, etc.; - add a nullable `package` option; - change the syntax for `extraConfig` (use `MISC` instead of `XDG_MISC_DIR`, the same way `xdg-user-dirs-update` works); and - add a `setSessionVariables` option, which defaults to `true` to maintain the current behavior. The last two changes are conditionalized on 25.11, so in future `XDG_MISC_DIR` will be disallowed and `setSessionVariables` will default to `false`.
1 parent 5f06cea commit 7facb3f

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

modules/misc/xdg-user-dirs.nix

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ in
3636
'';
3737
};
3838

39+
package = lib.mkPackageOption pkgs "xdg-user-dirs" { nullable = true; };
40+
3941
# Well-known directory list from
4042
# https://gitlab.freedesktop.org/xdg/xdg-user-dirs/blob/master/man/user-dirs.dirs.xml
4143

@@ -101,45 +103,76 @@ in
101103
defaultText = literalExpression "{ }";
102104
example = literalExpression ''
103105
{
104-
XDG_MISC_DIR = "''${config.home.homeDirectory}/Misc";
106+
MISC = "''${config.home.homeDirectory}/Misc";
105107
}
106108
'';
107-
description = "Other user directories.";
109+
description = ''
110+
Other user directories.
111+
112+
Prior to 25.11, these can be named like `XDG_MISC_DIR`.
113+
'';
108114
};
109115

110116
createDirectories = lib.mkEnableOption "automatic creation of the XDG user directories";
117+
118+
setSessionVariables = mkOption {
119+
type = with types; bool;
120+
default = lib.versionOlder config.home.stateVersion "25.11";
121+
defaultText = literalExpression ''
122+
lib.versionOlder config.home.stateVersion "25.11"
123+
'';
124+
description = ''
125+
Whether to set the XDG user dir environment variables, like
126+
`XDG_DESKTOP_DIR`. The recommended way to get these values is via the
127+
`xdg-user-dir` command or by processing
128+
`$XDG_CONFIG_HOME/user-dirs.dirs` directly in your application.
129+
130+
This defaults to `true` for state version < 25.11 and `false` otherwise.
131+
'';
132+
};
111133
};
112134

113135
config =
114136
let
115137
directories =
116138
(lib.filterAttrs (n: v: !isNull v) {
117-
XDG_DESKTOP_DIR = cfg.desktop;
118-
XDG_DOCUMENTS_DIR = cfg.documents;
119-
XDG_DOWNLOAD_DIR = cfg.download;
120-
XDG_MUSIC_DIR = cfg.music;
121-
XDG_PICTURES_DIR = cfg.pictures;
122-
XDG_PUBLICSHARE_DIR = cfg.publicShare;
123-
XDG_TEMPLATES_DIR = cfg.templates;
124-
XDG_VIDEOS_DIR = cfg.videos;
139+
DESKTOP = cfg.desktop;
140+
DOCUMENTS = cfg.documents;
141+
DOWNLOAD = cfg.download;
142+
MUSIC = cfg.music;
143+
PICTURES = cfg.pictures;
144+
PUBLICSHARE = cfg.publicShare;
145+
TEMPLATES = cfg.templates;
146+
VIDEOS = cfg.videos;
125147
})
126-
// cfg.extraConfig;
148+
// (
149+
if lib.versionOlder config.home.stateVersion "25.11" then
150+
lib.mapAttrs' (
151+
k:
152+
let
153+
name = lib.match "XDG_(.*)_DIR" k;
154+
in
155+
lib.nameValuePair (if name == null then k else lib.elemAt name 0)
156+
) cfg.extraConfig
157+
else
158+
cfg.extraConfig
159+
);
160+
161+
bindings = lib.mapAttrs' (k: lib.nameValuePair "XDG_${k}_DIR") directories;
127162
in
128163
lib.mkIf cfg.enable {
129-
assertions = [
130-
(lib.hm.assertions.assertPlatform "xdg.userDirs" pkgs lib.platforms.linux)
131-
];
132-
133164
xdg.configFile."user-dirs.dirs".text =
134165
let
135166
# For some reason, these need to be wrapped with quotes to be valid.
136-
wrapped = lib.mapAttrs (_: value: ''"${value}"'') directories;
167+
wrapped = lib.mapAttrs (_: value: ''"${value}"'') bindings;
137168
in
138169
lib.generators.toKeyValue { } wrapped;
139170

140171
xdg.configFile."user-dirs.conf".text = "enabled=False";
141172

142-
home.sessionVariables = directories;
173+
home.packages = lib.mkIf (cfg.package != null) [ cfg.package ];
174+
175+
home.sessionVariables = lib.mkIf cfg.setSessionVariables bindings;
143176

144177
home.activation.createXdgUserDirectories = lib.mkIf cfg.createDirectories (
145178
let

0 commit comments

Comments
 (0)