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

with pkgs.lib;

let

  inherit (pkgs) nettools dhcp lib;

  # Don't start dhclient on explicitly configured interfaces.
  ignoredInterfaces = 
    map (i: i.name) (lib.filter (i: i ? ipAddress && i.ipAddress != "" ) config.networking.interfaces);

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

  dhclientExitHooks = pkgs.writeText "dhclient-exit-hooks"
    ''
      #echo "$reason" >> /tmp/dhcp-exit
      #echo "$exit_status" >> /tmp/dhcp-exit

      if test "$reason" = BOUND -o "$reason" = REBOOT; then
          ${pkgs.glibc}/sbin/nscd --invalidate hosts

          # Restart ntpd.  (The "ip-up" event below will trigger the
          # restart.)  We need to restart it to make sure that it will
          # actually do something: if ntpd cannot resolve the server
          # hostnames in its config file, then it will never do
          # anything ever again ("couldn't resolve ..., giving up on
          # it"), so we silently lose time synchronisation.
          ${config.system.build.upstart}/sbin/initctl stop ntpd
          
          ${config.system.build.upstart}/sbin/initctl emit -n ip-up
      fi

      if test "$reason" = EXPIRE -o "$reason" = RELEASE; then
          ${config.system.build.upstart}/sbin/initctl emit -n ip-down
      fi
    '';
  
in

{

  ###### interface

  options = {
  
    networking.useDHCP = mkOption {
      default = true;
      merge = mergeEnableOption;
      description = "
        Whether to use DHCP to obtain an IP adress and other
        configuration for all network interfaces that are not manually
        configured.
      ";
    };
  
  };


  ###### implementation
  
  config = mkIf config.networking.useDHCP {

    jobs.dhclient = 
      { startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        preStart =
          ''
            # dhclient barfs if /proc/net/if_inet6 doesn't exist.
            ${config.system.sbin.modprobe}/sbin/modprobe ipv6 || true
          '';

        script =
          ''
            # Determine the interface on which to start dhclient.
            interfaces=

            for i in $(cd /sys/class/net && ls -d *); do
                # Only run dhclient on interfaces of type ARPHRD_ETHER
                # (1), i.e. Ethernet.  Ignore peth* devices; on Xen,
                # they're renamed physical Ethernet cards used for
                # bridging.
                if [ "$(cat /sys/class/net/$i/type)" = 1 ]; then
                    if ! for j in ${toString ignoredInterfaces}; do echo $j; done | grep -F -x -q "$i" &&
                       ! echo "$i" | grep -x -q "peth.*";
		    then<commit_msg>* Don't run dhclient on Xen's vif* and tap* interfaces.<commit_after>{ config, pkgs, ... }:

with pkgs.lib;

let

  inherit (pkgs) nettools dhcp lib;

  # Don't start dhclient on explicitly configured interfaces.
  ignoredInterfaces = 
    map (i: i.name) (lib.filter (i: i ? ipAddress && i.ipAddress != "" ) config.networking.interfaces);

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

  dhclientExitHooks = pkgs.writeText "dhclient-exit-hooks"
    ''
      #echo "$reason" >> /tmp/dhcp-exit
      #echo "$exit_status" >> /tmp/dhcp-exit

      if test "$reason" = BOUND -o "$reason" = REBOOT; then
          ${pkgs.glibc}/sbin/nscd --invalidate hosts

          # Restart ntpd.  (The "ip-up" event below will trigger the
          # restart.)  We need to restart it to make sure that it will
          # actually do something: if ntpd cannot resolve the server
          # hostnames in its config file, then it will never do
          # anything ever again ("couldn't resolve ..., giving up on
          # it"), so we silently lose time synchronisation.
          ${config.system.build.upstart}/sbin/initctl stop ntpd
          
          ${config.system.build.upstart}/sbin/initctl emit -n ip-up
      fi

      if test "$reason" = EXPIRE -o "$reason" = RELEASE; then
          ${config.system.build.upstart}/sbin/initctl emit -n ip-down
      fi
    '';
  
in

{

  ###### interface

  options = {
  
    networking.useDHCP = mkOption {
      default = true;
      merge = mergeEnableOption;
      description = "
        Whether to use DHCP to obtain an IP adress and other
        configuration for all network interfaces that are not manually
        configured.
      ";
    };
  
  };


  ###### implementation
  
  config = mkIf config.networking.useDHCP {

    jobs.dhclient = 
      { startOn = "started network-interfaces";
        stopOn = "stopping network-interfaces";

        preStart =
          ''
            # dhclient barfs if /proc/net/if_inet6 doesn't exist.
            ${config.system.sbin.modprobe}/sbin/modprobe ipv6 || true
          '';

        script =
          ''
            # Determine the interface on which to start dhclient.
            interfaces=

            for i in $(cd /sys/class/net && ls -d *); do
                # Only run dhclient on interfaces of type ARPHRD_ETHER
                # (1), i.e. Ethernet.  Ignore peth* devices; on Xen,
                # they're renamed physical Ethernet cards used for
                # bridging.  Likewise for vif* and tap*.
                if [ "$(cat /sys/class/net/$i/type)" = 1 ]; then
                    if ! for j in ${toString ignoredInterfaces}; do echo $j; done | grep -F -x -q "$i" &&
                       ! echo "$i" | grep -x -q "peth.*\|vif.*\|tap.*";
		    then
{'fraction_non_alphanumeric': 0.11639731251134919, 'fraction_numerical': 0.001452696568004358, 'mean_word_length': 1.8989473684210527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4759677', 'n_tokens_mistral': 1749, 'n_tokens_neox': 1626, 'n_words': 519}