Systemd in nixos derivation.nix

I am trying to compile a go program.Alone with that I need to change that compiled binary to a systemd service.so for that i writtenmy derivation as:

{ lib, buildGoModule, go }:

  buildGoModule rec {
     pname = "nm";
     version =  "6.0.8-1";
     src = fetchGit {
        url = "ssh://builduerbot@mygiturl/repo";
        rev = "77f1e98543757481309a32f9600f970e576576e110e";
        ref = "v6.0.8";
      };
     subPackages = [ "nm" ];
     vendorSha256 = "sha256-z/a2YIcHC2cziFHH2056Y3XwUsjHowPQv93VicmF4+E=";
     }       
{
 systemd.services.nm = {
   description = "nm";
   serviceConfig = {
     Type = "simple";
     ExecStart = "/nix/store/wav3rxq70y4jwjqdfdfdhdddbbrm6crwy-nm-6.0.8-1/bin/nm";
     Restart = "on-failure";
   };
   wantedBy = [ "multi-user.target" ];
 };
}

but am getting error as
error: attempt to call something which is not a function but a set

at /home/username/nimmygitbuild/nm.nix:3:3:
2|
3| buildGoModule rec {
| ^
4| pname = “nm”;

Couple things:

  1. You need to make this a nixos module file (taking config, and pkgs as inputs, rather than just a derivation to be able to configure nixos modules.
  2. Then you’d want to change buildGoModule to pkgs.buildGoModule.
  3. You don’t manually copy in the store path, you’d instead capture the output of buildGoModule in a variable and then use that in the ExecStart line. (ExecStart = "${my_nm}/bin/nm";)

am newbie, can you please kindly explain me to make nixos module file.

something like this, can’t test right now, but hopefully this helps:

Personally I’d do something like:

  • stick that in a file next to my nixos config (service-nm.nix)
  • then add imports = [ ./service-nm.nix ]; to my nixos config file.

Note: see ref and rev in the src is a bit odd to me. I guess if it works, it works. Not sure of the semantics.

1 Like

Can we build this independently without adding to /etc/nixos/configuration.nix ?

You can build the package “separately”, but configuring the systemd service is ultimately part of the nixos configuration. You could separate the package definition and the service using it though, and build the package per-emptively as part of CI or whatever.

hi,
I want to add buildInputs =[ libpcap ];
how can I add correctly to the above file