liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

demo.nix (6347B)


      1 # This is an example configuration for a "typical" small office/home
      2 # router and wifi access point.
      3 
      4 # You need to copy it to another filename and change the configuration
      5 # wherever the text "EDIT" appears - please consult the tutorial
      6 # documentation for details.
      7 
      8 { config, pkgs, lib, ... } :
      9 let
     10   inherit (pkgs.liminix.services) bundle oneshot longrun;
     11   inherit (pkgs) serviceFns;
     12   # EDIT: you can pick your preferred RFC1918 address space
     13   # for NATted connections, if you don't like this one.
     14   ipv4LocalNet = "10.8.0";
     15   svc = config.system.service;
     16 
     17 in rec {
     18   boot = {
     19     tftp = {
     20       freeSpaceBytes = 3 * 1024 * 1024;
     21       serverip = "10.0.0.1";
     22       ipaddr = "10.0.0.8";
     23     };
     24   };
     25 
     26   imports = [
     27     ../modules/bridge
     28     ../modules/dhcp6c
     29     ../modules/dnsmasq
     30     ../modules/firewall
     31     ../modules/hostapd
     32     ../modules/network
     33     ../modules/ntp
     34     ../modules/ppp
     35     ../modules/ssh
     36     ../modules/vlan
     37     ../modules/wlan.nix
     38   ];
     39   rootfsType = "jffs2";
     40   hostname = "the-internet"; # EDIT
     41 
     42   services.hostap = svc.hostapd.build {
     43     interface = config.hardware.networkInterfaces.wlan;
     44     # EDIT: you will want to change the obvious things
     45     # here to values of your choice
     46     params = {
     47       ssid = "the-internet";
     48       channel = "1";
     49       country_code = "GB";
     50       wpa_passphrase = "not a real wifi password";
     51 
     52       hw_mode="g";
     53       ieee80211n = 1;
     54       auth_algs = 1; # 1=wpa2, 2=wep, 3=both
     55       wpa = 2;       # 1=wpa, 2=wpa2, 3=both
     56       wpa_key_mgmt = "WPA-PSK";
     57       wpa_pairwise = "TKIP CCMP";   # auth for wpa (may not need this?)
     58       rsn_pairwise = "CCMP";        # auth for wpa2
     59       wmm_enabled = 1;
     60     };
     61   };
     62 
     63   services.int = svc.network.address.build {
     64     interface = svc.bridge.primary.build { ifname = "int"; };
     65     family = "inet"; address = "${ipv4LocalNet}.1"; prefixLength = 16;
     66   };
     67 
     68   services.bridge =  svc.bridge.members.build {
     69     primary = services.int;
     70     members = with config.hardware.networkInterfaces;
     71       [ wlan lan ];
     72   };
     73 
     74   services.ntp = svc.ntp.build {
     75     pools = { "pool.ntp.org" = ["iburst"]; };
     76     makestep = { threshold = 1.0; limit = 3; };
     77   };
     78 
     79   services.sshd = svc.ssh.build { };
     80 
     81   users.root = {
     82     # EDIT: choose a root password and then use
     83     # "mkpasswd -m sha512crypt" to determine the hash.
     84     # It should start wirh $6$.
     85     passwd = "$6$6HG7WALLQQY1LQDE$428cnouMJ7wVmyK9.dF1uWs7t0z9ztgp3MHvN5bbeo0M4Kqg/u2ThjoSHIjCEJQlnVpDOaEKcOjXAlIClHWN21";
     86     openssh.authorizedKeys.keys = [
     87       # EDIT: you can add your ssh pubkey here
     88       # "ssh-rsa AAAAB3NzaC1....H6hKd user@example.com";
     89     ];
     90   };
     91 
     92   services.dns =
     93     let interface = services.int;
     94     in svc.dnsmasq.build {
     95       resolvconf = services.resolvconf;
     96       inherit interface;
     97       ranges = [
     98         "${ipv4LocalNet}.10,${ipv4LocalNet}.249"
     99         # EDIT: ... maybe. In this example we use "ra-stateless",
    100         # meaning dnsmasq sends router advertisements with the O and A
    101         # bits set, and provides a stateless DHCP service. The client
    102         # will use a SLAAC address, and use DHCP for other
    103         # configuration information.
    104         # If you didn't understand the preceding sentence then
    105         # the default is _probably_ fine, but if you need
    106         # a DHCP-only IPv6 network or some other different
    107         # configuration, this is the place to change it.
    108         "::,constructor:$(output ${interface} ifname),ra-stateless"
    109       ];
    110       # EDIT: choose a domain name for the DNS names issued for your
    111       # DHCP-issued hosts
    112       domain = "lan.example.com";
    113     };
    114 
    115   services.wan = svc.pppoe.build {
    116     interface = config.hardware.networkInterfaces.wan;
    117     ppp-options = [
    118       "debug" "+ipv6" "noauth"
    119       # EDIT: change the strings "chap-username"
    120       # and "chap-secret" to match the username/password
    121       # provided by your ISP for PPP logins
    122       "name" "chap-username"
    123       "password" "chap-secret"
    124     ];
    125   };
    126 
    127   services.resolvconf = oneshot rec {
    128     dependencies = [ services.wan ];
    129     name = "resolvconf";
    130     up = ''
    131       . ${serviceFns}
    132       ( in_outputs ${name}
    133        echo "nameserver $(output ${services.wan} ns1)" > resolv.conf
    134        echo "nameserver $(output ${services.wan} ns2)" >> resolv.conf
    135        chmod 0444 resolv.conf
    136       )
    137     '';
    138   };
    139 
    140   filesystem =
    141     let inherit (pkgs.pseudofile) dir symlink;
    142     in dir {
    143       etc = dir {
    144         "resolv.conf" = symlink "${services.resolvconf}/.outputs/resolv.conf";
    145       };
    146     };
    147 
    148   services.defaultroute4 = svc.network.route.build {
    149     via = "$(output ${services.wan} address)";
    150     target = "default";
    151     dependencies = [ services.wan ];
    152   };
    153 
    154   services.defaultroute6 = svc.network.route.build {
    155     via = "$(output ${services.wan} ipv6-peer-address)";
    156     target = "default";
    157     interface = services.wan;
    158   };
    159 
    160   services.firewall = svc.firewall.build {
    161     ruleset = import ./demo-firewall.nix;
    162   };
    163 
    164   services.packet_forwarding = svc.network.forward.build { };
    165 
    166   # We expect the ISP uses DHCP6 to issue IPv6 addresses. There is a
    167   # service to request address information in the form of a DHCP
    168   # lease, and two dependent services that listen for updates to the
    169   # DHCP address information and update the addresses of the WAN and
    170   # LAN interfaces respectively.
    171 
    172   services.dhcp6c =
    173     let client = svc.dhcp6c.client.build {
    174           interface = services.wan;
    175         };
    176     in bundle {
    177       name = "dhcp6c";
    178       contents = [
    179         (svc.dhcp6c.prefix.build {
    180           # if your ISP provides you a real IPv6 prefix for your local
    181           # network (usually a /64 or /48 or something in between the
    182           # two), this service subscribes to that "prefix delegation"
    183           # information, and uses it to assign an address to the LAN
    184           # device. dnsmasq will notice this address and use it to
    185           # form the addresses it hands out to devices on the lan
    186           inherit client;
    187           interface = services.int;
    188         })
    189         (svc.dhcp6c.address.build {
    190           # if your ISP provides you a regular global IPv6 address,
    191           # this service subscribes to that information and assigns
    192           # the address to the WAN device.
    193           inherit client;
    194           interface = services.wan;
    195         })
    196       ];
    197     };
    198 
    199   defaultProfile.packages = with pkgs; [
    200     min-collect-garbage
    201   ];
    202 }