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

No way to instantiate systemd template units #108054

Closed
hmenke opened this issue Dec 31, 2020 · 4 comments
Closed

No way to instantiate systemd template units #108054

hmenke opened this issue Dec 31, 2020 · 4 comments
Labels
0.kind: bug Something is broken

Comments

@hmenke
Copy link
Member

hmenke commented Dec 31, 2020

Describe the bug

Systemd has the notion of template units, e.g. [email protected] is such a unit. These template units contain common configuration that is suppose to be shared among all instances of this template. For example on a typical Linux distribution it would be possible to instantiate the aforementioned unit for a particular machine

[root@arch] # systemctl enable [email protected]
Created symlink /etc/systemd/system/machines.target.wants/[email protected] → /etc/systemd/system/[email protected].

Something similar is currently not possible on NixOS. Trying to use

{
  systemd.services."systemd-nspawn@archlinux".enable = true;
}

will create an entirely new unit from scratch, completely ignoring the existence of the template [email protected]. Even worse, because /etc/systemd/system links into the read-only Nix store, it's not even possible to instantiate the template in a stateful manner:

[root@nixos] # systemctl enable [email protected]
Failed to enable unit: Unit file /etc/systemd/system/machines.target.wants/[email protected] does not exis.

To Reproduce
Steps to reproduce the behavior:

  1. Add systemd.services."systemd-nspawn@archlinux".enable = true; to /etc/nixos/configuration.nix.
  2. Rebuild
  3. Compare the output of systemctl cat [email protected] and systemctl cat [email protected] and see that they have nothing to do with each other.

Expected behavior
Template units are a powerful concept of systemd and it would be beneficial if NixOS users could take advantage of it.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Notify maintainers

The systemd module doesn't have a maintainer field, so I'm pinging the systemd package maintainers: @andir @eelco @flokli

Metadata
Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

  • system: "x86_64-linux"
  • host os: Linux 5.9.16, NixOS, 20.09.2405.e065200fc90 (Nightingale)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.4pre20201201_5a6ddb3
  • nixpkgs: /etc/channels/nixos

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module: systemd
@hmenke hmenke added the 0.kind: bug Something is broken label Dec 31, 2020
@hmenke
Copy link
Member Author

hmenke commented Dec 31, 2020

Possible duplicate of #80933

@flokli
Copy link
Contributor

flokli commented Dec 31, 2020

yes, dup of #80933.

@flokli flokli closed this as completed Dec 31, 2020
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/declaring-instances-of-a-generic-systemd-service-e-g-my-service-foo/11738/2

@hmenke
Copy link
Member Author

hmenke commented Apr 16, 2021

Here is a workaround for those that are interested:

{ config, lib, pkgs, ... }: {

  systemd.packages = [
    (pkgs.runCommandNoCC "machines" {
      preferLocalBuild = true;
      allowSubstitutes = false;
    } ''
      mkdir -p $out/etc/systemd/system/
      ln -s /etc/systemd/system/[email protected] $out/etc/systemd/system/[email protected]
    '')
  ];

  systemd.services."systemd-nspawn@archlinux".wantedBy = [ "machines.target" ];

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.kind: bug Something is broken
Projects
None yet
Development

No branches or pull requests

3 participants