NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.86k stars 13.93k forks source link

Improve NixOS error messages for systemd.services config #275296

Open crabdancing opened 10 months ago

crabdancing commented 10 months ago

Describe the bug

Trying to make a systemd oneshot remain after exit, results in strange build errors:

       error: A definition for option `systemd.services.helloWorld.serviceConfig' is not of type `attribute set of (systemd option)'. Definition values:
       - In `/nix/store/7fkwd086mmlkpmsk1xyn23h4rlak8qrz-source/flake.nix':
           {
             RemainAfterExit = "yes";
             Restart = "on-error";
             Type = "oneshot";
           }

Steps To Reproduce

Steps to reproduce the behavior:

  systemd.services.helloWorld = {
    script =
    ''
      echo "Hello World"
    '';
    enable = true;
    description = "example";
    wantedBy = [ "multi-user.target" ];
    serviceConfig = {
      Type = "oneshot";
      Restart = "on-error";
      RemainAfterExit = "yes";
    };
  };

Expected behavior

Should be able to make a oneshot that remains after exit.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

Add any other context about the problem here.

Notify maintainers

@nbraud

Metadata

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

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.2, NixOS, 23.11 (Tapir), 23.11.20231128.7c4c205`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.1`
 - channels(root): `""`
 - nixpkgs: `/etc/nixPath`
eclairevoyant commented 10 months ago

The Restart value on-error is invalid, see man systemd.service for valid values.

crabdancing commented 10 months ago

Ah, right you are. It's on-failure, not on-error.

crabdancing commented 10 months ago

I'm just curious... is there any way to get the Nix code spec of the schema to print what specifically the is wrong with it? Are there any features planned for this -- that is, better error messages when types do not match?

lytedev commented 3 months ago

I agree with above -- that would be very cool. For myself, I was able to troubleshoot by examining the relevant systemd man pages. In my case, I am configuring systemd-networkd, so man systemd.network had the fields and values well-documented. In this case, you could look at man systemd.service.

crabdancing commented 3 months ago

It's kind of unfortunate because, although having a formalized schema is the 'correct' approach, and can in principle have huge advantages, the end result when doing that with Nix is often something harder to troubleshoot than if it just deployed the broken config, a lot of the time. :P

eclairevoyant commented 3 months ago

You're welcome to reopen the ticket if you want to track improving the error messages. I don't see any open issues for this specifically, nor for submodules overall.

crabdancing commented 3 months ago

Sure! :)