1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-03-31 04:04:32 +00:00

xdg-autostart: Add readOnly option (#6629)

When `readOnly` is set to `true` the autostart entries are linked from
a readonly directory in the nix store and `XDG_CONFIG_HOME/autostart` is
a link to that directory, so that programs cannot install arbitrary
autostart services.
This commit is contained in:
Olmo Kramer 2025-03-30 18:22:16 +02:00 committed by GitHub
parent 1d2ed9c503
commit 09280e17bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 64 additions and 11 deletions

View file

@ -1,25 +1,31 @@
{ config, lib, ... }:
{ config, lib, pkgs, ... }:
let
inherit (builtins) baseNameOf listToAttrs map unsafeDiscardStringContext;
inherit (lib) literalExpression mkEnableOption mkIf mkOption types;
cfg = config.xdg.autostart;
/* "/nix/store/x-foo/application.desktop" -> {
name = "autostart/application.desktop";
value = { source = "/nix/store/x-foo/application.desktop"; };
}
*/
mapDesktopEntry = entry: {
name = "autostart/${unsafeDiscardStringContext (baseNameOf entry)}";
value.source = entry;
};
linkedDesktopEntries = pkgs.runCommandNoCCLocal "xdg-autostart-entries" { } ''
mkdir -p $out
${lib.concatMapStringsSep "\n" (e: "ln -s ${e} $out") cfg.entries}
'';
in {
meta.maintainers = with lib.maintainers; [ Scrumplex ];
options.xdg.autostart = {
enable = mkEnableOption "creation of XDG autostart entries";
readOnly = mkOption {
type = lib.types.bool;
description = ''
Make `XDG_CONFIG_HOME/autostart` a symlink to a readonly directory so that
programs cannot install arbitrary autostart services.
'';
default = false;
example = true;
};
entries = mkOption {
type = with types; listOf path;
description = ''
@ -35,6 +41,9 @@ in {
};
config = mkIf (cfg.enable && cfg.entries != [ ]) {
xdg.configFile = listToAttrs (map mapDesktopEntry cfg.entries);
xdg.configFile.autostart = {
source = linkedDesktopEntries;
recursive = !cfg.readOnly;
};
};
}

View file

@ -0,0 +1,41 @@
{ pkgs, ... }:
{
xdg.autostart = {
enable = true;
readOnly = true;
entries = [
"${pkgs.test1}/share/applications/test1.desktop"
"${pkgs.test2}/share/applications/test2.desktop"
];
};
test.stubs = {
test1 = {
outPath = null;
buildScript = ''
mkdir -p $out/share/applications
echo test1 > $out/share/applications/test1.desktop
'';
};
test2 = {
outPath = null;
buildScript = ''
mkdir -p $out/share/applications
echo test2 > $out/share/applications/test2.desktop
'';
};
};
nmt.script = ''
assertLinkExists home-files/.config/autostart
assertFileExists home-files/.config/autostart/test1.desktop
assertFileContent home-files/.config/autostart/test1.desktop \
${pkgs.test1}/share/applications/test1.desktop
assertFileExists home-files/.config/autostart/test2.desktop
assertFileContent home-files/.config/autostart/test2.desktop \
${pkgs.test2}/share/applications/test2.desktop
'';
}

View file

@ -27,6 +27,8 @@
};
nmt.script = ''
assertDirectoryExists home-files/.config/autostart
assertFileExists home-files/.config/autostart/test1.desktop
assertFileContent home-files/.config/autostart/test1.desktop \
${pkgs.test1}/share/applications/test1.desktop

View file

@ -3,4 +3,5 @@
xdg-default-locations = ./default-locations.nix;
xdg-mime-disabled = ./mime-disabled.nix;
xdg-autostart = ./autostart.nix;
xdg-autostart-readonly = ./autostart-readonly.nix;
}