IfState: Difference between revisions

add dhcpv4
m Change link to current site wiki
 
(3 intermediate revisions by one other user not shown)
Line 4: Line 4:


=== Examples ===
=== Examples ===
You can find several examples on the [https://ifstate.net/2.0/examples/ IfState website]. Some include NixOS configuration instructions, while the more complex examples are covered in detail here.
You can find several examples on the [https://ifstate.net/2.2/examples/ IfState website]. Some include NixOS configuration instructions, while the more complex examples are covered in detail here.


==== Network Namespaces (netns) ====
==== Network Namespaces (netns) ====
Line 15: Line 15:
   systemd.services."<name>".serviceConfig.NetworkNamespacePath = "/var/run/netns/<netnsName>";
   systemd.services."<name>".serviceConfig.NetworkNamespacePath = "/var/run/netns/<netnsName>";
}
}
</syntaxhighlight>When using [https://nixos.wiki/wiki/NixOS_Containers nixos-containers], network namespaces allow you to configure the network outside the container. This separation simplifies management and ensures the container’s network setup is independent of its internal configuration.<syntaxhighlight lang="nixos">
</syntaxhighlight>When using [[NixOS Containers|nixos-containers]], network namespaces allow you to configure the network outside the container. This separation simplifies management and ensures the container’s network setup is independent of its internal configuration.<syntaxhighlight lang="nixos">
{
{
   containers."<name>".networkNamespace = "/var/run/netns/<netnsName>";
   containers."<name>".networkNamespace = "/var/run/netns/<netnsName>";
Line 167: Line 167:
<syntaxhighlight lang="nixos">
<syntaxhighlight lang="nixos">
{ lib, pkgs, ... }:
{ lib, pkgs, ... }:
let
  script = pkgs.writeScript "ifstate-udhcp-script.sh" ''
    #! /bin/sh
    if [ "$1" = bound ]; then
      ${lib.getExe' pkgs.iproute2 "ip"} address add "$ip/$mask" dev "$interface"
      if [ -n "$mtu" ]; then
        ${lib.getExe' pkgs.iproute2 "ip"} link set mtu "$mtu" dev "$interface"
      fi
      if [ -n "$router" ]; then
        ${lib.getExe' pkgs.iproute2 "ip"} route add default via "$router" dev "$interface"
      fi
    fi
  '';
in
{
{
   networking.ifstate = {
   networking.ifstate = {
Line 186: Line 172:
     settings = {
     settings = {
       parameters.hooks.dhcp.script = pkgs.writeScript "ifstate-udhcp-wrapper-script.sh" ''
       parameters.hooks.dhcp.script = pkgs.writeScript "ifstate-udhcp-wrapper-script.sh" ''
         ${lib.getExe' pkgs.busybox "udhcpc"} --quit --now -i $IFS_IFNAME -b --script ${script}
         ${lib.getExe' pkgs.busybox "udhcpc"} --quit --now -i $IFS_IFNAME -b --script ${pkgs.busybox}/default.script
       '';
       '';
       interfaces.eth1 = {
       interfaces.eth1 = {
         addresses = [ ];
         addresses = [ ];
         hooks = [
         hooks = [
           {
           { name = "dhcp"; }
            name = "dhcp";
          }
         ];
         ];
         link = {
         link = {