Packages without options in configuration.nix

I’ll preface this by saying I’m a nix noob…

I’m trying to get package net-snmp going. There doesn’t seem to be any way to control it from configuration.nix. I can specify it as a package so it’s installed, but I can’t include any config files embedded in the configuration.nix, nor start it automatically. Is that common? The /var/log/net-snmp.log file refers to a snmpconf perl program do build a config but that wasn’t included in the package.

I’m sure I can get it going the old fashioned way, I’m just trying to do it the nix way. (also because it makes it harder to deploy a server)

1 Like

I found this article that looks like it would be a solution to my issue…for creating arbitrary files in /etc:

Now I just need to figure out how to start the daemon at boot-up and how to pass arguments to it like:
snmpd -c /etc/snmp/snmpd.conf

1 Like

This should do well: systemd.services..script

Not having any luck. This is what I have so far, please tell me if I’m on the right track:

systemd.services = {
snmpd = {
description = “Net-SNMP daemon”;
wantedBy = [ “multi-user.target” ];
after = [ “network.target” ];
restartIfChanged = true;

   serviceConfig = {
     User = "root";
     Group = "root";
     Restart = "always";
     ExecStart = "${pkgs.net-snmp}/bin/snmpd -c /etc/snmp/snmpd.conf";
   };
 };

};

systemd.services.snmpd.enable = true;

also tried this:

systemd.services.snmpd.script = “/bin/snmpd -c /etc/snmp/snmpd.conf”;
systemd.services.snmpd.enable = true;

I got a bit farther with this… in a separate snmpd.nix:

{
  environment.systemPackages = with pkgs; [
    net-snmp                                                                                                                             
  ];     
 
  systemd.services.snmpd = {
    enable = true;
    wantedBy = [ "multi-user.target" ];
    description = "Net-SNMP daemon"; 
    after = [ "network.target" ];
    restartIfChanged = true;
    serviceConfig = {
      User = "root";
      Group = "root";     
      Restart = "always"; 
      ExecStart = "${pkgs.net-snmp}/bin/snmpd -Le -f -c /etc/snmp/snmpd.conf";
    }; 
  };   
  
  environment.etc."snmp/snmpd.conf".text = lib.mkForce ''                                                                                
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
rouser SNMPv3User priv .1
access  notConfigGroup ""      any       noauth    exact  systemview none none
syslocation [redacted]
syscontact Root <root@[redacted]>
dontLogTCPWrappersConnects yes
#  '';  
 
  systemd.tmpfiles.rules = [
    "d /etc/snmp         0644 root root -"
    "d /var/lib/net-snmp 0600 root root -"
  ];    
}

This nix file “works”, with a running snmpd, but isn’t enough to actually connect to it. The rpouser directive provided is targeted at SNMPv3. However, for SNMP v3, the users need to be created either by putting createUser statements in the /etc/snmp/snmpd.conf (ugly), or by running the net-snmp-create-v3-user command for each user. I haven’t managed to get the command part worked out, so if anyone has pointers, that would be awesome!

Alternative may be to use activationScripts or userActivationScripts.
It would also run less frequently than a systemd service which would run on each boot (or more).

 system.userActivationScripts = {
    my-script = {
      text = ''
        # some script...
      '';
    };
  };