diff --git a/modules/misc/xdg-mime-apps.nix b/modules/misc/xdg-mime-apps.nix new file mode 100644 index 00000000..ac675d2d --- /dev/null +++ b/modules/misc/xdg-mime-apps.nix @@ -0,0 +1,88 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.xdg.mimeApps; + + strListOrSingleton = with types; + coercedTo (either (listOf str) str) toList (listOf str); + +in + +{ + meta.maintainers = with maintainers; [ pacien ]; + + options.xdg.mimeApps = { + enable = mkOption { + type = types.bool; + default = false; + description = '' + Whether to manage $XDG_CONFIG_HOME/mimeapps.list. + + + The generated file is read-only. + ''; + }; + + # descriptions from + # https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html + + associations.added = mkOption { + type = types.attrsOf strListOrSingleton; + default = { }; + example = literalExample '' + { + "mimetype1" = [ "foo1.desktop" "foo2.desktop" "foo3.desktop" ]; + "mimetype2" = "foo4.desktop"; + } + ''; + description = '' + Defines additional associations of applications with + mimetypes, as if the .desktop file was listing this mimetype + in the first place. + ''; + }; + + associations.removed = mkOption { + type = types.attrsOf strListOrSingleton; + default = { }; + example = { "mimetype1" = "foo5.desktop"; }; + description = '' + Removes associations of applications with mimetypes, as if the + .desktop file was not listing this + mimetype in the first place. + ''; + }; + + defaultApplications = mkOption { + type = types.attrsOf strListOrSingleton; + default = { }; + example = literalExample '' + { + "mimetype1" = [ "default1.desktop" "default2.desktop" ]; + } + ''; + description = '' + The default application to be used for a given mimetype. This + is, for instance, the one that will be started when + double-clicking on a file in a file manager. If the + application is no longer installed, the next application in + the list is attempted, and so on. + ''; + }; + }; + + config = mkIf cfg.enable { + xdg.configFile."mimeapps.list".text = + let + joinValues = mapAttrs (n: concatStringsSep ";"); + in + generators.toINI {} { + "Added Associations" = joinValues cfg.associations.added; + "Removed Associations" = joinValues cfg.associations.removed; + "Default Applications" = joinValues cfg.defaultApplications; + }; + }; +} diff --git a/modules/modules.nix b/modules/modules.nix index 0e187959..c4261182 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -33,8 +33,9 @@ let (loadModule ./misc/qt.nix { }) (loadModule ./misc/submodule-support.nix { }) (loadModule ./misc/version.nix { }) - (loadModule ./misc/xdg.nix { }) + (loadModule ./misc/xdg-mime-apps.nix { condition = hostPlatform.isLinux; }) (loadModule ./misc/xdg-user-dirs.nix { condition = hostPlatform.isLinux; }) + (loadModule ./misc/xdg.nix { }) (loadModule ./programs/afew.nix { }) (loadModule ./programs/alacritty.nix { }) (loadModule ./programs/alot.nix { }) diff --git a/tests/default.nix b/tests/default.nix index ef63c7bf..355427ac 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -34,6 +34,7 @@ import nmt { i3-keybindings = ./modules/services/window-managers/i3-keybindings.nix; } // import ./modules/misc/pam + // import ./modules/misc/xdg // import ./modules/misc/xsession // import ./modules/programs/firefox // import ./modules/programs/rofi diff --git a/tests/modules/misc/xdg/default.nix b/tests/modules/misc/xdg/default.nix new file mode 100644 index 00000000..5772becd --- /dev/null +++ b/tests/modules/misc/xdg/default.nix @@ -0,0 +1,3 @@ +{ + xdg-mime-apps-basics = ./mime-apps-basics.nix; +} diff --git a/tests/modules/misc/xdg/mime-apps-basics-expected.ini b/tests/modules/misc/xdg/mime-apps-basics-expected.ini new file mode 100644 index 00000000..c27181eb --- /dev/null +++ b/tests/modules/misc/xdg/mime-apps-basics-expected.ini @@ -0,0 +1,9 @@ +[Added Associations] +mimetype1=foo1.desktop;foo2.desktop;foo3.desktop +mimetype2=foo4.desktop + +[Default Applications] +mimetype1=default1.desktop;default2.desktop + +[Removed Associations] +mimetype1=foo5.desktop diff --git a/tests/modules/misc/xdg/mime-apps-basics.nix b/tests/modules/misc/xdg/mime-apps-basics.nix new file mode 100644 index 00000000..2c320710 --- /dev/null +++ b/tests/modules/misc/xdg/mime-apps-basics.nix @@ -0,0 +1,30 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + config = { + xdg.mimeApps = { + enable = true; + associations = { + added = { + "mimetype1" = [ "foo1.desktop" "foo2.desktop" "foo3.desktop" ]; + "mimetype2" = "foo4.desktop"; + }; + removed = { + mimetype1 = "foo5.desktop"; + }; + }; + defaultApplications = { + "mimetype1" = [ "default1.desktop" "default2.desktop" ]; + }; + }; + + nmt.script = '' + assertFileExists home-files/.config/mimeapps.list + assertFileContent \ + home-files/.config/mimeapps.list \ + ${./mime-apps-basics-expected.ini} + ''; + }; +}