nix-community / home-manager

Manage a user environment using Nix [maintainer=@rycee]
https://nix-community.github.io/home-manager/
MIT License
6.63k stars 1.74k forks source link

Control user services on non-NixOS #4972

Open khlopkov opened 7 months ago

khlopkov commented 7 months ago

Description

I use Home Manager on a non-NixOS (Debian in my case). Home Manager installed few systemd services with a packages into ~/.nix-profile/share/systemd/user/ from nixpkgs. Those services can not be found, for example this command:

systemctl --user restart xdg-desktop-portal-gtk

gives the following output:

Failed to restart xdg-desktop-portal-gtk.service: Unit xdg-desktop-portal-gtk.service not found.

I was able to fix this by adding a symlink from the `~/.nix-profile/share/systemd to the ~/.config/systemd:

ln -s ~/.nix-profile/share/systemd/ ~/.config/systemd

But there are few issues with this workaround:

  1. I can not enable those services from my Home Manager configuration and have to enable them manually.
  2. It conflicts with the xdg.portal.enable (and I suppose with other services that can be enabled through the Home Manager). It prints following when trying to set this option to true:
    Existing file '/home/etsilence/.config/systemd/user/xdg-desktop-portal.service' is in the way of '/nix/store/d3yw2n212zbz7473pbwwc41dj1ipkk8x-home-manager-files/.config/systemd/user/xdg-desktop-portal.service'

It can be fixed by linking specific services into :

ln -s ~/.nix-profile/share/systemd/user/xdg-desktop-portal-gtk.service ~/.config/systemd/user/xdg-desktop-portal-gtk.service

But in this case I have to symlink each required service

There is a similar issue: https://github.com/nix-community/home-manager/issues/4922, but I started a new one, because this thread is about more wide problem rather just a problem with a xdg.portal.enable option. Will it be possible to control over systemd.user services installed from nixpkgs on non NixOS environments?

khlopkov commented 7 months ago

@baloo from the linked issue's thread suggested adding this activation script into home-manager's configuration:

  home.activation.linkSystemd = let
    inherit (lib) hm;
  in hm.dag.entryBefore [ "reloadSystemd" ] (''
    find $HOME/.config/systemd/user/ \
      -type l \
      -exec bash -c "readlink {} | grep -q $HOME/.nix-profile/share/systemd/user/" \; \
      -delete

    find $HOME/.nix-profile/share/systemd/user/ \
      -type f -o -type l \
      -exec ln -s {} $HOME/.config/systemd/user/ \;
  '');

Is it possible to merge something similar to the home-manager master branch?

The decision above, LGTM, because it should replace only those files controlled by Home Manager. But it will also replace custom services declared by user via home-manager. Also, this script should fail in case when user manually (not by Home Manager) added a .service file into $HOME/.config/systemd/user/ and it is also declared in $HOME/.nix-profile/share/systemd/user/. So I am not sure this specific script can be merged into a master branch.

I probably would like to contribute this change into Home Manager, but I think it needs some discussion before

khlopkov commented 7 months ago

Probably that's out of scope of the issue, but I think there are also some other files that should be linked into user's home: .application files of packages, dbus interfaces and services

stale[bot] commented 4 months ago

Thank you for your contribution! I marked this issue as stale due to inactivity. Please be considerate of people watching this issue and receiving notifications before commenting 'I have this issue too'. We welcome additional information that will help resolve this issue. Please read the relevant sections below before commenting.

If you are the original author of the issue

* If this is resolved, please consider closing it so that the maintainers know not to focus on this. * If this might still be an issue, but you are not interested in promoting its resolution, please consider closing it while encouraging others to take over and reopen an issue if they care enough. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

If you are not the original author of the issue

* If you are also experiencing this issue, please add details of your situation to help with the debugging process. * If you know how to solve the issue, please consider submitting a Pull Request that addresses this issue.

Memorandum on closing issues

Don't be afraid to manually close an issue, even if it holds valuable information. Closed issues stay in the system for people to search, read, cross-reference, or even reopen – nothing is lost! Closing obsolete issues is an important way to help maintainers focus their time and effort.

CyberShadow commented 1 month ago

Why does home-manager create some unit file links in ~/.config/systemd/user, and some in ~/.nix-profile/share/systemd/user?

Maybe it would be simpler to make it create everything in ~/.nix-profile/share/systemd/user, then on non-NixOS, ~/.config/systemd/user could be symlinked to it?