Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xdg-mime type package options #5920

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions modules/misc/xdg-mime.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,50 @@ with lib;
let

cfg = config.xdg.mime;
inherit (lib) getExe getExe';

in {
options = {
xdg.mime.enable = mkOption {
type = types.bool;
default = pkgs.stdenv.hostPlatform.isLinux;
defaultText =
literalExpression "true if host platform is Linux, false otherwise";
description = ''
Whether to install programs and files to support the
XDG Shared MIME-info specification and XDG MIME Applications
specification at
<https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html>
and
<https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-latest.html>,
respectively.
'';
xdg.mime = {
enable = mkOption {
type = types.bool;
default = pkgs.stdenv.hostPlatform.isLinux;
defaultText =
literalExpression "true if host platform is Linux, false otherwise";
description = ''
Whether to install programs and files to support the
XDG Shared MIME-info specification and XDG MIME Applications
specification at
<https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html>
and
<https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-latest.html>,
respectively.
'';
};

sharedMimeInfoPackage = mkOption {
type = types.package;
default = pkgs.shared-mime-info;
defaultText = literalExpression "pkgs.shared-mime-info";
description = "The package to use when running update-mime-database.";
};

desktopFileUtilsPackage = mkOption {
type = types.package;
default = pkgs.desktop-file-utils;
defaultText = literalExpression "pkgs.desktop-file-utils";
description =
"The package to use when running update-desktop-database.";
};
};
};

config = mkIf config.xdg.mime.enable {
config = mkIf cfg.enable {
assertions =
[ (hm.assertions.assertPlatform "xdg.mime" pkgs platforms.linux) ];

home.packages = [
# Explicitly install package to provide basic mime types.
pkgs.shared-mime-info
cfg.sharedMimeInfoPackage

# Make sure the target directories will be real directories.
(pkgs.runCommandLocal "dummy-xdg-mime-dirs1" { } ''
Expand All @@ -46,12 +63,12 @@ in {
if [[ -w $out/share/mime && -w $out/share/mime/packages && -d $out/share/mime/packages ]]; then
XDG_DATA_DIRS=$out/share \
PKGSYSTEM_ENABLE_FSYNC=0 \
${pkgs.buildPackages.shared-mime-info}/bin/update-mime-database \
${getExe cfg.sharedMimeInfoPackage} \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are run during the build right?, so this breaks cross. Reported by @colemickens

-V $out/share/mime > /dev/null
fi

if [[ -w $out/share/applications ]]; then
${pkgs.buildPackages.desktop-file-utils}/bin/update-desktop-database \
${getExe' cfg.desktopFileUtilsPackage "update-desktop-database"} \
$out/share/applications
fi
'';
Expand Down
3 changes: 3 additions & 0 deletions tests/modules/misc/xdg/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
xdg-default-locations = ./default-locations.nix;
xdg-user-dirs-null = ./user-dirs-null.nix;
xdg-portal = ./portal.nix;
xdg-mime = ./mime.nix;
xdg-mime-disabled = ./mime-disabled.nix;
xdg-mime-package = ./mime-packages.nix;
}
10 changes: 10 additions & 0 deletions tests/modules/misc/xdg/mime-disabled.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ ... }: {
config = {
xdg.mime.enable = false;
nmt.script = ''
# assert that neither application is run
assertPathNotExists home-path/share/applications/mimeinfo.cache
assertPathNotExists home-path/share/applications/mime
'';
};
}
3 changes: 3 additions & 0 deletions tests/modules/misc/xdg/mime-expected.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[MIME Cache]
text/html=mime-test.desktop;
text/xml=mime-test.desktop;
38 changes: 38 additions & 0 deletions tests/modules/misc/xdg/mime-packages.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{ config, ... }:
let inherit (config.lib.test) mkStubPackage;
in {
config = {
xdg.mime.enable = true;
xdg.mime.sharedMimeInfoPackage = mkStubPackage {
name = "update-mime-database";
buildScript = ''
mkdir -p $out/bin
echo '#!/bin/sh' > $out/bin/update-mime-database
echo 'mkdir -p $out/share/mime && touch $out/share/mime/mime.cache' >> $out/bin/update-mime-database
chmod +x $out/bin/update-mime-database
'';
};
xdg.mime.desktopFileUtilsPackage = mkStubPackage {
name = "desktop-file-utils";
buildScript = ''
mkdir -p $out/bin
echo '#!/bin/sh' > $out/bin/update-desktop-database
echo 'mkdir -p $out/share/applications/ && ln -s ${
./mime-expected.cache
} $out/share/applications/mimeinfo.cache' >> $out/bin/update-desktop-database
chmod +x $out/bin/update-desktop-database
'';
};
nmt.script = ''
assertFileExists home-path/share/applications/mimeinfo.cache # Check that update-desktop-database created file
# Check that update-desktop-database file matches expected
assertFileContent \
home-path/share/applications/mimeinfo.cache \
${./mime-expected.cache}

assertDirectoryExists home-path/share/mime # Check that update-mime-database created directory
assertFileExists home-path/share/mime/mime.cache # Check that update-mime-database created file

'';
};
}
24 changes: 24 additions & 0 deletions tests/modules/misc/xdg/mime.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ ... }: {
config = {
xdg.mime.enable = true;
xdg.desktopEntries = {
mime-test = { # mime info test
name = "mime-test";
mimeType = [ "text/html" "text/xml" ];
};

};

nmt.script = ''
assertFileExists home-path/share/applications/mimeinfo.cache # Check that update-desktop-database created file
# Check that update-desktop-database file matches expected
assertFileContent \
home-path/share/applications/mimeinfo.cache \
${./mime-expected.cache}

assertDirectoryExists home-path/share/mime # Check that update-mime-database created directory
assertDirectoryNotEmpty home-path/share/mime # Check that update-mime-database created files

'';
};
}