<commit_before>{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.dhcpd;

  stateDir = "/var/lib/dhcp"; # Don't use /var/state/dhcp; not FHS-compliant.

  configFile = if cfg.configFile != null then cfg.configFile else pkgs.writeText "dhcpd.conf"
    ''
      default-lease-time 600;
      max-lease-time 7200;
      authoritative;
      ddns-update-style interim;
      log-facility local1; # see dhcpd.nix

      ${cfg.extraConfig}

      ${lib.concatMapStrings
          (machine: ''
            host ${machine.hostName} {
              hardware ethernet ${machine.ethernetAddress};
              fixed-address ${machine.ipAddress};
            }
          '')
          cfg.machines
      }
    '';

in

{

  ###### interface

  options = {

    services.dhcpd = {

      enable = mkOption {
        default = false;
        description = "
          Whether to enable the DHCP server.
        ";
      };

      extraConfig = mkOption {
        default = "";
        example = ''
          option subnet-mask 255.255.255.0;
          option broadcast-address 192.168.1.255;
          option routers 192.168.1.5;
          option domain-name-servers 192.168.3.11, 172.16.31.10, 172.16.31.10;
          option domain-name "example.org";
          subnet 192.168.1.0 netmask 255.255.255.0 {
            range 192.168.1.100 192.168.1.200;
          }
        '';
        description = "
          Extra text to be appended to the DHCP server configuration
          file.  Currently, you almost certainly need to specify
          something here, such as the options specifying the subnet
          mask, DNS servers, etc.
        ";
      };

      configFile = mkOption {
        default = null;
        description = "
          The path of the DHCP server configuration file.  If no file
          is specified, a file is generated using the other options.
        ";
      };

      interfaces = mkOption {
        default = ["eth0"];
        description = "
          The interfaces on which the DHCP server should listen.
        ";
      };

      machines = mkOption {
        default = [];
        example = [
          { hostName = "foo";
            ethernetAddress = "00:16:76:9a:32:1d";
            ipAddress = "192.168.1.10";
          }
          { hostName = "bar";
            ethernetAddress = "00:19:d1:1d:c4:9a";
            ipAddress = "192.168.1.11";
          }
        ];
        description = "
          A list mapping ethernet addresses to IP addresses for the
          DHCP server.
        ";
      };

    };

  };


  ###### implementation

  config = mkIf config.services.dhcpd.enable {

    users = {
      extraUsers.dhcpd = {
        uid = config.ids.uids.dhcpd;
        description = "DHCP daemon user";
      };
    };

    jobs.dhcpd =
      { description = "DHCP server";

        startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        script =
          ''
            mkdir -m 755 -p ${stateDir}

            touch ${stateDir}/dhcpd.leases

            exec ${pkgs.dhcp}/sbin/dhcpd -f --no-pid -cf ${configFile} \
                -lf ${stateDir}/dhcpd.leases -user dhcpd -group nogroup \
                ${toString cfg.interfaces}
          '';
      };

  };

}<commit_msg>nixos/dhcpd: Convert to systemd from upstart<commit_after>{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.dhcpd;

  stateDir = "/var/lib/dhcp"; # Don't use /var/state/dhcp; not FHS-compliant.

  configFile = if cfg.configFile != null then cfg.configFile else pkgs.writeText "dhcpd.conf"
    ''
      default-lease-time 600;
      max-lease-time 7200;
      authoritative;
      ddns-update-style interim;
      log-facility local1; # see dhcpd.nix

      ${cfg.extraConfig}

      ${lib.concatMapStrings
          (machine: ''
            host ${machine.hostName} {
              hardware ethernet ${machine.ethernetAddress};
              fixed-address ${machine.ipAddress};
            }
          '')
          cfg.machines
      }
    '';

in

{

  ###### interface

  options = {

    services.dhcpd = {

      enable = mkOption {
        default = false;
        description = "
          Whether to enable the DHCP server.
        ";
      };

      extraConfig = mkOption {
        default = "";
        example = ''
          option subnet-mask 255.255.255.0;
          option broadcast-address 192.168.1.255;
          option routers 192.168.1.5;
          option domain-name-servers 192.168.3.11, 172.16.31.10, 172.16.31.10;
          option domain-name "example.org";
          subnet 192.168.1.0 netmask 255.255.255.0 {
            range 192.168.1.100 192.168.1.200;
          }
        '';
        description = "
          Extra text to be appended to the DHCP server configuration
          file.  Currently, you almost certainly need to specify
          something here, such as the options specifying the subnet
          mask, DNS servers, etc.
        ";
      };

      configFile = mkOption {
        default = null;
        description = "
          The path of the DHCP server configuration file.  If no file
          is specified, a file is generated using the other options.
        ";
      };

      interfaces = mkOption {
        default = ["eth0"];
        description = "
          The interfaces on which the DHCP server should listen.
        ";
      };

      machines = mkOption {
        default = [];
        example = [
          { hostName = "foo";
            ethernetAddress = "00:16:76:9a:32:1d";
            ipAddress = "192.168.1.10";
          }
          { hostName = "bar";
            ethernetAddress = "00:19:d1:1d:c4:9a";
            ipAddress = "192.168.1.11";
          }
        ];
        description = "
          A list mapping ethernet addresses to IP addresses for the
          DHCP server.
        ";
      };

    };

  };


  ###### implementation

  config = mkIf config.services.dhcpd.enable {

    users = {
      extraUsers.dhcpd = {
        uid = config.ids.uids.dhcpd;
        description = "DHCP daemon user";
      };
    };

    systemd.services.dhcpd =
      { description = "DHCP server";

        wantedBy = [ "multi-user.target" ];

        path = [ pkgs.dhcp ];

        preStart =
          ''
            mkdir -m 755 -p ${stateDir}

            touch ${stateDir}/dhcpd.leases

            mkdir -m 755 -p /run/dhcpd
            chown dhcpd /run/dhcpd
          '';

        serviceConfig =
          { ExecStart = "@${pkgs.dhcp}/sbin/dhcpd dhcpd"
              + " -pf /run/dhcpd/dhcpd.pid -cf ${configFile}"
              + " -lf ${stateDir}/dhcpd.leases -user dhcpd -group nogroup"
              + " ${toString cfg.interfaces}";
            Restart = "always";
            Type = "forking";
            PIDFile = "/run/dhcpd/dhcpd.pid";
          };
      };

  };

}
{'fraction_non_alphanumeric': 0.11636735289823331, 'fraction_numerical': 0.04161191414805081, 'mean_word_length': 1.5230202578268877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 24, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4111675', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2034, 'n_words': 489}