How to create system user

Hi guys! I have a question, how do I create a system user? I am trying to install and run Clamav on my NixOS machine as a systemd unit. This is my confg so far:

  environment.systemPackages = with pkgs; [
    pkgs.clamav
  ];

and systemd:

  systemd = {
    services = {
      clamd = {
        description = "ClamAV Daemon";
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          ExecStart = "${pkgs.clamav}/bin/clamd --foreground=yes";
          Restart = "on-failure";
          User = "clamav";
          Group = "clamav";
          PrivateTmp = true;
          RuntimeDirectory = "clamav";
          RuntimeDirectoryMode = "0755";
        };
      };
      freshclam = {
        description = "ClamAV Virus Database Updater";
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
          ExecStart = "${pkgs.clamav}/bin/freshclam --foreground=yes";
          Restart = "on-failure";
          User = "clamav";
          Group = "clamav";
          PrivateTmp = true;
          RuntimeDirectory = "clamav";
          RuntimeDirectoryMode = "0755";
        };
      };
    };
  };

I was following this post:

I can see that there is no user named clamav. How do I create one?

Would this work for you?

users.groups."clamav" = {};
users.users."clamav" = {
    group = "clamav";
    isSystemUser = true;
};
1 Like

Yes! Thank you, although I didn’t use double quotes (except for group = “clamav”;).

The nixos module can do this for you.

https://search.nixos.org/options?channel=24.11&from=0&size=50&sort=relevance&type=packages&query=services.clamav

https://github.com/NixOS/nixpkgs/blob/cbd8ec4de4469333c82ff40d057350c30e9f7d36/nixos/modules/services/security/clamav.nix

Example usage:

services.clamav = {
  daemon.enable = true;
  updater = {
    enable = true;
    interval = "daily";
  };
};

Oh, thank you! I’ll try it on my other machine.

PS if you need help, it’s good to ask the question in the Help category, so that it’s clear to readers instantly and you get some additional options like being able to mark a solution.

Oh, sorry. I’ll do that next time. Thank you for reminding.