Skip to content

Commit c3f3957

Browse files
committed
xdg.userDirs: change the syntax for extraConfig
E.g., use `MISC` instead of `XDG_MISC_DIR`, the same way `xdg-user-dirs-update` works. This is conditionalized on 25.11, so in future `XDG_MISC_DIR` will be disallowed.
1 parent cda6f9e commit c3f3957

File tree

4 files changed

+97
-13
lines changed

4 files changed

+97
-13
lines changed

modules/misc/xdg-user-dirs.nix

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,14 @@ in
103103
defaultText = literalExpression "{ }";
104104
example = literalExpression ''
105105
{
106-
XDG_MISC_DIR = "''${config.home.homeDirectory}/Misc";
106+
MISC = "''${config.home.homeDirectory}/Misc";
107107
}
108108
'';
109-
description = "Other user directories.";
109+
description = ''
110+
Other user directories.
111+
112+
Prior to 25.11, these should be named like `XDG_MISC_DIR`.
113+
'';
110114
};
111115

112116
createDirectories = lib.mkEnableOption "automatic creation of the XDG user directories";
@@ -132,30 +136,43 @@ in
132136
let
133137
directories =
134138
(lib.filterAttrs (n: v: !isNull v) {
135-
XDG_DESKTOP_DIR = cfg.desktop;
136-
XDG_DOCUMENTS_DIR = cfg.documents;
137-
XDG_DOWNLOAD_DIR = cfg.download;
138-
XDG_MUSIC_DIR = cfg.music;
139-
XDG_PICTURES_DIR = cfg.pictures;
140-
XDG_PUBLICSHARE_DIR = cfg.publicShare;
141-
XDG_TEMPLATES_DIR = cfg.templates;
142-
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;
143147
})
144-
// 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;
145162
in
146163
lib.mkIf cfg.enable {
147164
xdg.configFile."user-dirs.dirs".text =
148165
let
149166
# For some reason, these need to be wrapped with quotes to be valid.
150-
wrapped = lib.mapAttrs (_: value: ''"${value}"'') directories;
167+
wrapped = lib.mapAttrs (_: value: ''"${value}"'') bindings;
151168
in
152169
lib.generators.toKeyValue { } wrapped;
153170

154171
xdg.configFile."user-dirs.conf".text = "enabled=False";
155172

156173
home.packages = lib.mkIf (cfg.package != null) [ cfg.package ];
157174

158-
home.sessionVariables = lib.mkIf cfg.setSessionVariables directories;
175+
home.sessionVariables = lib.mkIf cfg.setSessionVariables bindings;
159176

160177
home.activation.createXdgUserDirectories = lib.mkIf cfg.createDirectories (
161178
let

tests/modules/misc/xdg/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
xdg-mime-disabled = ./mime-disabled.nix;
55
xdg-autostart = ./autostart.nix;
66
xdg-autostart-readonly = ./autostart-readonly.nix;
7+
xdg-user-dirs-mixed = ./user-dirs-mixed.nix;
78
xdg-user-dirs-null = ./user-dirs-null.nix;
9+
xdg-user-dirs-short = ./user-dirs-short.nix;
810
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
config,
3+
pkgs,
4+
...
5+
}:
6+
7+
{
8+
config = {
9+
home.stateVersion = "25.05";
10+
11+
xdg.userDirs = {
12+
enable = true;
13+
extraConfig.PROJECTS = "${config.home.homeDirectory}/Projects";
14+
## This will stop working with stateVersion 25.11.
15+
extraConfig.XDG_MISC_DIR = "${config.home.homeDirectory}/Misc";
16+
};
17+
18+
nmt.script = ''
19+
configFile=home-files/.config/user-dirs.dirs
20+
assertFileExists $configFile
21+
assertFileContent $configFile ${pkgs.writeText "expected" ''
22+
XDG_DESKTOP_DIR="/home/hm-user/Desktop"
23+
XDG_DOCUMENTS_DIR="/home/hm-user/Documents"
24+
XDG_DOWNLOAD_DIR="/home/hm-user/Downloads"
25+
XDG_MISC_DIR="/home/hm-user/Misc"
26+
XDG_MUSIC_DIR="/home/hm-user/Music"
27+
XDG_PICTURES_DIR="/home/hm-user/Pictures"
28+
XDG_PROJECTS_DIR="/home/hm-user/Projects"
29+
XDG_PUBLICSHARE_DIR="/home/hm-user/Public"
30+
XDG_TEMPLATES_DIR="/home/hm-user/Templates"
31+
XDG_VIDEOS_DIR="/home/hm-user/Videos"
32+
''}
33+
'';
34+
};
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
config,
3+
pkgs,
4+
...
5+
}:
6+
7+
{
8+
config = {
9+
xdg.userDirs = {
10+
enable = true;
11+
extraConfig.PROJECTS = "${config.home.homeDirectory}/Projects";
12+
};
13+
14+
nmt.script = ''
15+
configFile=home-files/.config/user-dirs.dirs
16+
assertFileExists $configFile
17+
assertFileContent $configFile ${pkgs.writeText "expected" ''
18+
XDG_DESKTOP_DIR="/home/hm-user/Desktop"
19+
XDG_DOCUMENTS_DIR="/home/hm-user/Documents"
20+
XDG_DOWNLOAD_DIR="/home/hm-user/Downloads"
21+
XDG_MUSIC_DIR="/home/hm-user/Music"
22+
XDG_PICTURES_DIR="/home/hm-user/Pictures"
23+
XDG_PROJECTS_DIR="/home/hm-user/Projects"
24+
XDG_PUBLICSHARE_DIR="/home/hm-user/Public"
25+
XDG_TEMPLATES_DIR="/home/hm-user/Templates"
26+
XDG_VIDEOS_DIR="/home/hm-user/Videos"
27+
''}
28+
'';
29+
};
30+
}

0 commit comments

Comments
 (0)