diff --git a/hosts/physiques/thot/default.nix b/hosts/physiques/thot/default.nix index 27f44b4..af8bacc 100644 --- a/hosts/physiques/thot/default.nix +++ b/hosts/physiques/thot/default.nix @@ -5,6 +5,7 @@ ./hardware-configuration.nix ./networking.nix ./nginx.nix + ./restic.nix ]; networking.hostId = "bbdd1133"; diff --git a/hosts/physiques/thot/info.nix b/hosts/physiques/thot/info.nix index e45b6db..e46cfc5 100644 --- a/hosts/physiques/thot/info.nix +++ b/hosts/physiques/thot/info.nix @@ -2,16 +2,16 @@ name = "thot"; description = "Serveur de backup sous nixos utilisant Restic"; id = 14; - + enable = true; - networking.adm.enable = false; - resticClient.enable = false; - - services = { - resticServer = { - enable = true; - port = 4242; - }; + networking = { + adm.enable = false; }; + + monitoring = { + nginx.enable = true; + }; + + resticClient.enable = false; } diff --git a/hosts/physiques/thot/restic.nix b/hosts/physiques/thot/restic.nix new file mode 100644 index 0000000..16b7936 --- /dev/null +++ b/hosts/physiques/thot/restic.nix @@ -0,0 +1,8 @@ +{ + config.crans.services = { + resticServer = { + enable = true; + port = 4242; + }; + }; +} diff --git a/hosts/vm/collabora/info.nix b/hosts/vm/collabora/info.nix index c68b81f..47ba48c 100644 --- a/hosts/vm/collabora/info.nix +++ b/hosts/vm/collabora/info.nix @@ -9,5 +9,9 @@ srv-nat.enable = true; }; + monitoring = { + nginx.enable = true; + }; + resticClient.enable = false; } diff --git a/hosts/vm/coturn/info.nix b/hosts/vm/coturn/info.nix index 115c65f..7d99a69 100644 --- a/hosts/vm/coturn/info.nix +++ b/hosts/vm/coturn/info.nix @@ -9,10 +9,14 @@ adm.enable = true; srv = { enable = true; - ipv4 = "185.230.79.19"; + ipv4.address = "185.230.79.19"; }; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "06:07"; services = { diff --git a/hosts/vm/coturn/livekit.nix b/hosts/vm/coturn/livekit.nix index e636e35..8a1a320 100644 --- a/hosts/vm/coturn/livekit.nix +++ b/hosts/vm/coturn/livekit.nix @@ -5,7 +5,7 @@ # secrets de turn. # -{ config, pkgs, lib, ... }: +{ config, info, pkgs, lib, ... }: let keyFile = "/run/livekit.key"; @@ -24,7 +24,7 @@ in rtc = { use_external_ip = false; - node_ip = config.crans.networking.srv.ipv4; + node_ip = info.networking.srv.ipv4.address; tcp_port = 8101; port_range_start = 50000; port_range_end = 52000; diff --git a/hosts/vm/jitsi/info.nix b/hosts/vm/jitsi/info.nix index f070613..3b2b3f9 100644 --- a/hosts/vm/jitsi/info.nix +++ b/hosts/vm/jitsi/info.nix @@ -9,17 +9,17 @@ adm.enable = true; srv = { enable = true; - ipv4 = "185.230.79.15"; + ipv4.address = "185.230.79.15"; }; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "02:34"; services = { acme.enable = true; }; - - - public-ssh = ""; - } diff --git a/hosts/vm/livre/info.nix b/hosts/vm/livre/info.nix index 1632dd6..ccd17a0 100644 --- a/hosts/vm/livre/info.nix +++ b/hosts/vm/livre/info.nix @@ -9,5 +9,9 @@ srv-nat.enable = true; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "03:45"; } diff --git a/hosts/vm/mediakiwi/info.nix b/hosts/vm/mediakiwi/info.nix index 3097f89..7f13c3f 100644 --- a/hosts/vm/mediakiwi/info.nix +++ b/hosts/vm/mediakiwi/info.nix @@ -9,5 +9,9 @@ srv-nat.enable = true; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "06:47"; } diff --git a/hosts/vm/neo/info.nix b/hosts/vm/neo/info.nix index c181629..07716d6 100644 --- a/hosts/vm/neo/info.nix +++ b/hosts/vm/neo/info.nix @@ -8,10 +8,14 @@ networking = { srv = { enable = true; - ipv4 = "185.230.79.5"; + ipv4.address = "185.230.79.5"; }; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "04:56"; services = { diff --git a/hosts/vm/nextcloud/info.nix b/hosts/vm/nextcloud/info.nix index 598f79a..f66456c 100644 --- a/hosts/vm/nextcloud/info.nix +++ b/hosts/vm/nextcloud/info.nix @@ -18,5 +18,9 @@ homeAdh.enable = true; + monitoring = { + nginx.enable = true; + }; + resticClient.enable = false; } diff --git a/hosts/vm/periodique/info.nix b/hosts/vm/periodique/info.nix index 489eaff..532b5a6 100644 --- a/hosts/vm/periodique/info.nix +++ b/hosts/vm/periodique/info.nix @@ -9,5 +9,9 @@ srv-nat.enable = true; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "02:56"; } diff --git a/hosts/vm/reverseproxy/info.nix b/hosts/vm/reverseproxy/info.nix index e2bcf3f..da7699d 100644 --- a/hosts/vm/reverseproxy/info.nix +++ b/hosts/vm/reverseproxy/info.nix @@ -10,10 +10,15 @@ srv = { enable = true; interface = "ens20"; - ipv4 = "185.230.79.42"; + ipv4.address = "185.230.79.42"; }; }; - resticClient.when = "03:42"; + services.acme.enable = true; + monitoring = { + nginx.enable = true; + }; + + resticClient.when = "03:42"; } diff --git a/hosts/vm/reverseproxy/reverseproxy.nix b/hosts/vm/reverseproxy/reverseproxy.nix index 4fe964d..488e7d4 100644 --- a/hosts/vm/reverseproxy/reverseproxy.nix +++ b/hosts/vm/reverseproxy/reverseproxy.nix @@ -230,10 +230,6 @@ in "zero".proxyPass = "172.16.10.130"; }; }; - - services = { - acme.enable = true; - }; }; # Règle particulières diff --git a/hosts/vm/vaultwarden/info.nix b/hosts/vm/vaultwarden/info.nix index 1cf8489..beaa0f1 100644 --- a/hosts/vm/vaultwarden/info.nix +++ b/hosts/vm/vaultwarden/info.nix @@ -9,5 +9,9 @@ srv-nat.enable = true; }; + monitoring = { + nginx.enable = true; + }; + resticClient.when = "04:44"; } diff --git a/infra/default.nix b/infra/default.nix new file mode 100644 index 0000000..e375a62 --- /dev/null +++ b/infra/default.nix @@ -0,0 +1,5 @@ +{ + # XXX: Transformer en module NixOs + vlans = import ./vlans.nix; + switchs = import ./switchs.nix; +} diff --git a/infra/switchs.nix b/infra/switchs.nix new file mode 100644 index 0000000..6e374ae --- /dev/null +++ b/infra/switchs.nix @@ -0,0 +1,6 @@ +[ + "arceus" + "carapuce" + "salameche" + "horst" +] diff --git a/infra/vlans.nix b/infra/vlans.nix new file mode 100644 index 0000000..c37a7f6 --- /dev/null +++ b/infra/vlans.nix @@ -0,0 +1,124 @@ +{ + + srv = { + id = 2; + description = "Réseau public"; + interface = "ens19"; + ipv4 = { + address = "185.230.79.0"; + prefixLength = 26; + routes = [ + { + address = "0.0.0.0"; + via = "185.230.79.62"; + prefixLength = 0; + } + ]; + }; + ipv6 = { + address = "2a0c:700:10::"; + routes = [ + { + address = "::"; + via = "2a0c:700:2::ff:fe00:9902"; + prefixLength = 0; + } + ]; + }; + }; + + srv-nat = { + id = 3; + description = "Réseau derrière un NAT"; + interface = "ens19"; + ipv4.routes = [ + { + address = "0.0.0.0"; + via = "172.16.3.99"; + prefixLength = 0; + } + ]; + ipv6 = { + address = "2a0c:700:3::"; + routes = [ + { + address = "::"; + via = "2a0c:700:3::ff:fe00:9903"; + prefixLength = 0; + } + ]; + }; + }; + + san = { + id = 4; + description = "Accès aux baies de stockages"; + interface = "ens19"; + }; + + ceph = { + id = 6; + description = "Réseau interne à ceph"; + interface = "ens20"; + }; + + adm = { + id = 10; + description = "Réseau interne"; + interface = "ens18"; + }; + + adh = { + id = 12; + description = "Réseau adhérent"; + ipv4 = { + address = "185.230.78.0"; + routes = [ + { + address = "0.0.0.0"; + via = "185.230.78.12"; + prefixLength = 0; + } + ]; + }; + ipv6 = { + address = "2a0c:700:12::"; + prefixLength = 48; + routes = [ + { + address = "::"; + via = "2a0c:700:12::ff:fe00:9912"; + prefixLength = 0; + } + ]; + }; + }; + + adh-adm = { + id = 13; + description = "Réseau d’administration du réseau adhérent"; + }; + + ens = { + id = 2751; + description = "Interconnexion avec l’ENS."; + ipv4 = { + address = "138.231.136.0"; + prefixLength = 29; + }; + ipv6 = null; + }; + + ens-clubs = { + id = 2754; + description = "Réseau clubs interconnecté avec l’ENS"; + ipv6 = null; + }; + + ens-lp = { + id = 2756; + description = "Réseau imprimante interconnecté avec l’ENS"; + ipv6 = null; + }; + +} diff --git a/lib/info.nix b/lib/info.nix index b689add..b9ffef8 100644 --- a/lib/info.nix +++ b/lib/info.nix @@ -3,11 +3,6 @@ let inherit (lib) attrNames - attrsToList - isInt - isString - join - length listToAttrs filter filterAttrs @@ -16,31 +11,9 @@ let nixosSystem readDir warn - warnIf - warnIfNot ; - infoRequired = { - name = isString; - description = isString; - id = isInt; - }; - verifyAttrs = - info: - let - missing = filter (n: !(info ? ${n})) (attrNames infoRequired); - wrongType = filter (n: info ? ${n.name} && !(n.value info.${n.name})) (attrsToList infoRequired); - in - (warnIf ( - length missing > 0 - ) "${info.name}/info.nix : les attributs « ${join ", " missing} » sont manquants") - (warnIf (length wrongType > 0) - "${info.name}/info.nix : les attributs « ${join ", " (map (i: i.name) wrongType)} » sont mal typés" - ) - (warnIfNot ( - (info.isDebian or false) || info ? enable - ) "${info.name}/info.nix : cette machine nix ne définit pas l’attribut enable") - info; + infra = import ../infra; get_hosts_names = path: attrNames (filterAttrs (name: type: type == "directory") (readDir path)); get_info = path: @@ -51,7 +24,15 @@ let filePath = path + "/${name}/info.nix"; in if builtins.pathExists filePath then - verifyAttrs (import filePath) + (lib.evalModules { + modules = [ + ../modules/info + filePath + ]; + specialArgs = { + inherit infra; + }; + }).config else warn "${toString filePath} not found" null ) (get_hosts_names path) @@ -67,10 +48,10 @@ rec { nixosSystem { specialArgs = inputs // { hosts = all_hosts; + inherit info infra; }; modules = [ (path + "/${name}") - ({ ... }: { config.crans = info; }) ] ++ baseModules; } diff --git a/modules/crans/default.nix b/modules/crans/default.nix index 4f28495..ee6e5aa 100644 --- a/modules/crans/default.nix +++ b/modules/crans/default.nix @@ -1,13 +1,12 @@ -{ lib, config, ... }: +{ + lib, + info, + ... +}: let - cfg = config.crans; - inherit (lib) - mkEnableOption - mkOption mkIf - types optional ; in @@ -21,76 +20,22 @@ in ./ntp.nix ./restic_client.nix ./monitoring.nix + ./motd.nix ./nullmailer.nix ./packages.nix ./ssh.nix ./store.nix ./users.nix - ./vlans.nix ./virtualisation.nix ]; - options.crans = { - enable = mkEnableOption "Configuration commune à toutes les machines du Crans"; - - name = mkOption { - type = types.str; - example = "vm-test"; - description = "Nom de la machine"; - }; - - isVm = mkOption { - type = types.bool; - description = "Est-ce une machine virtuelle ?"; - }; - - isDebian = mkOption { - type = types.bool; - default = false; - example = "true"; - description = "Est-ce une machine Debian ?"; - }; - - description = mkOption { - type = types.str; - example = "VM de test"; - description = "Description de la machine"; - }; - - id = mkOption { - type = types.int; - example = 135; - description = '' - ID de la machine : - - < 100 machine physique - - >= 100 VM ( id de la VM dans proxmox ) - ''; - }; - - public-ssh = mkOption { - type = types.str; - description = "Clé ssh public de la machine"; - }; - }; - - config = mkIf cfg.enable { - crans = { - homeNounou.enable = lib.mkDefault true; - monitoring.enable = true; - networking = { - enable = lib.mkDefault true; - adm.enable = lib.mkDefault true; - }; - resticClient.enable = lib.mkDefault true; - users = { - ldap.enable = true; - }; - }; + # Options ../info/meta.nix + config = mkIf info.enable { warnings = ( optional ( - cfg.isVm != (cfg.id >= 100) - ) "${cfg.name}: isVm (${toString cfg.isVm}) est incohérent avec son id (${toString cfg.id})" + info.isVm != (info.id >= 100) + ) "${info.name}: isVm (${toString info.isVm}) est incohérent avec son id (${toString info.id})" ); }; } diff --git a/modules/crans/home.nix b/modules/crans/home.nix index e2e4bab..57cc051 100644 --- a/modules/crans/home.nix +++ b/modules/crans/home.nix @@ -1,24 +1,14 @@ -{ lib, config, ... }: +{ lib, info, ... }: let - cfg = config.crans; - - inherit (lib) mkEnableOption mkIf; + inherit (lib) mkIf; in { - options.crans = { - homeNounou = { - enable = mkEnableOption "Monter /home_nounou."; - }; - homeAdh = { - enable = mkEnableOption "Monter /home-adh"; - }; - }; - + # Options ../info/home.nix config = { fileSystems = { - home_nounou = mkIf cfg.homeNounou.enable { + home_nounou = mkIf info.homeNounou.enable { mountPoint = "/home_nounou"; device = "172.16.10.1:/pool/home"; fsType = "nfs"; @@ -28,10 +18,10 @@ in ]; }; - home_adh = mkIf cfg.homeAdh.enable { + home_adh = mkIf info.homeAdh.enable { mountPoint = "/home-adh"; device = "172.16.4.2:/pool/home"; - fsType = "nfs"; + fsType = "nfs"; }; }; }; diff --git a/modules/crans/monitoring.nix b/modules/crans/monitoring.nix index 18862cf..f0ee161 100644 --- a/modules/crans/monitoring.nix +++ b/modules/crans/monitoring.nix @@ -1,32 +1,28 @@ -{ config, lib, ... }: +{ + config, + info, + lib, + ... +}: let - cfg = config.crans.monitoring; + cfg = info.monitoring; - inherit (lib) - mkEnableOption - mkIf - mkOption - types - ; + nginxEnable = config.services.nginx.enable; + inherit (lib) mkIf optional; + + boolToStr = b: if b then "true" else "false"; in { - options.crans.monitoring = { - enable = mkEnableOption "Monitoring prometheus de la machine."; + # Options ../info/monitoring.nix - nginx = { - enable = mkOption { - type = types.bool; - default = config.services.nginx.enable; - example = true; - description = "Monitoring de Nginx par prometheus."; - }; - }; - }; + config = { + warnings = optional (cfg.enable && (nginxEnable != cfg.nginx.enable)) '' + ${info.name} : config.services.nginx.enable (${boolToStr nginxEnable}) n’est pas cohérent avec le monitoring déclaré dans l’info.nix (${boolToStr cfg.nginx.enable}) + ''; - config = mkIf cfg.enable { - services.prometheus.exporters = { + services.prometheus.exporters = mkIf cfg.enable { node = { enable = true; port = 9100; diff --git a/modules/crans/motd.nix b/modules/crans/motd.nix new file mode 100644 index 0000000..d23a54a --- /dev/null +++ b/modules/crans/motd.nix @@ -0,0 +1,19 @@ +{ self, lib, ... }: +let + inherit (lib) removeSuffix; +in +{ + users.motd = + let + shortRev = self.shortRev or self.dirtyShortRev or "inconnue"; + rev = self.rev or (removeSuffix "-dirty" (self.dirtyRev or "inconnue")); + isDirty = !(self ? rev); + in + '' + ${ + if isDirty then "!! DIRTY !! " else "" + }Version déployée ${shortRev}, modifiée le ${self.lastModifiedDate or "??"}. + + https://gitlab.crans.org/nounous/nixos/-/commit/${rev} + ''; +} diff --git a/modules/crans/networking.nix b/modules/crans/networking.nix index 99e20b1..e3cb361 100644 --- a/modules/crans/networking.nix +++ b/modules/crans/networking.nix @@ -1,91 +1,26 @@ { lib, - config, - hosts, + infra, + info, ... }: let - cfg = config.crans; - cfg-net = cfg.networking; + cfg-net = info.networking; inherit (lib) - mkEnableOption mkIf - mkOption mkDefault - types - mod - mapAttrs mapAttrs' filterAttrs - mergeAttrsList - optional - fixedWidthString nameValuePair ; in { - options.crans.networking = { - enable = mkEnableOption "Configuration réseaux commune à toutes les machines du Crans."; - } - // mapAttrs ( - vlan: conf: - let - vlanId = mod conf.id 100; - in - mergeAttrsList ( - [ - { - enable = mkEnableOption "Activation du réseau ${vlan}"; - - interface = mkOption { - type = types.str; - description = "Interface pour le réseau ${vlan}"; - default = conf.interface; - }; - - defaultRoutes = mkOption { - type = types.bool; - default = true; - description = "Configuration par défaut des routes de ${vlan}"; - }; - } - ] - ++ optional ((conf.ipv4 or true) != null) { - ipv4 = mkOption { - type = types.str; - description = "Adresse IPv4 de la machine sur le réseau ${vlan}"; - default = "172.16.${toString vlanId}.${toString cfg.id}"; - }; - } - ++ optional ((conf.ipv6 or true) != null) ( - let - # XXX: Utilisation du masque pour le déterminer à la place ? - prefix = - if lib.hasPrefix "2a0c:700" conf.ipv6.address then - "2a0c:700" - else if lib.hasPrefix "fd00:0:0" conf.ipv6.address then - "fd00:0:0" - else - (builtins.warn "Le prefixe de ${conf.ipv6.address} n’est pas reconnu"); - in - { - ipv6 = mkOption { - type = types.str; - description = "Adresse IPv6 de la machine sur le réseau ${vlan}"; - default = "${prefix}:${toString vlanId}::ff:fe0${toString (cfg.id / 100)}:${toString (mod cfg.id 100)}${ - fixedWidthString 2 "0" (toString vlanId) - }"; - }; - } - ) - ) - ) cfg.vlans; - + # Options ../info/networking.nix config = mkIf cfg-net.enable { networking = { - hostName = cfg.name; + hostName = info.name; useDHCP = false; firewall.enable = mkDefault false; nameservers = [ @@ -95,24 +30,24 @@ in ]; interfaces = mapAttrs' ( - vlan: vconf: + vname: _: let - conf = cfg-net.${vlan}; + conf = cfg-net.${vname}; in nameValuePair conf.interface ( # On itère ipv4 / ipv6 builtins.mapAttrs ( ipvx: _: - mkIf (vconf.${ipvx} != null) { + mkIf (conf.${ipvx} != null) { addresses = [ { - address = conf.${ipvx}; - prefixLength = vconf.${ipvx}.prefixLength; + address = conf.${ipvx}.address; + prefixLength = conf.${ipvx}.prefixLength; } ]; - routes = mkIf conf.defaultRoutes vconf.${ipvx}.routes; + routes = conf.${ipvx}.routes; } ) { @@ -120,7 +55,7 @@ in "ipv6" = null; } ) - ) (filterAttrs (n: _: cfg-net.${n}.enable) cfg.vlans); # On filtre les interfaces désactivées. + ) (filterAttrs (n: _: cfg-net.${n}.enable) infra.vlans); # On filtre les interfaces désactivées. }; }; } diff --git a/modules/crans/restic_client.nix b/modules/crans/restic_client.nix index b0b3228..05f530c 100644 --- a/modules/crans/restic_client.nix +++ b/modules/crans/restic_client.nix @@ -1,40 +1,18 @@ -{ config, lib, ... }: +{ + config, + info, + lib, + ... +}: let - cfg = config.crans.resticClient; + cfg = info.resticClient; - inherit (lib) - mkEnableOption - mkIf - mkOption - types - ; + inherit (lib) mkIf; in { - options.crans.resticClient = { - enable = mkEnableOption "Configuration générale pour le client restic."; - - additionalPaths = mkOption { - type = types.listOf types.path; - default = [ ]; - example = [ "/backup" ]; - description = "Chemins à backuper en plus de ceux par défaut."; - }; - - additionalExcludes = mkOption { - type = types.listOf types.path; - default = [ ]; - example = [ "/var/lib//cache" ]; - description = "Chemins à exclure des backups en plus de ceux par défaut."; - }; - - when = mkOption { - type = types.str; - example = "05:42"; - description = "À quelle heure faire les backups."; - }; - }; + # Options ../info/resticClient.nix config = mkIf cfg.enable { age.secrets = { @@ -52,11 +30,13 @@ in paths = [ "/etc" "/var" - ] ++ cfg.additionalPaths; + ] + ++ cfg.additionalPaths; exclude = [ "/var/cache" "/var/lib/lxcfs" - ] ++ cfg.additionalExcludes; + ] + ++ cfg.additionalExcludes; timerConfig = { OnCalendar = cfg.when; RandomizedDelaySec = "6h"; diff --git a/modules/crans/users.nix b/modules/crans/users.nix index 9c14880..02802ee 100644 --- a/modules/crans/users.nix +++ b/modules/crans/users.nix @@ -1,35 +1,9 @@ -{ - config, - lib, - self, - ... -}: - +{ config, info, ... }: let - cfg = config.crans.users; - - inherit (lib) - mkEnableOption - mkOption - types - ; + cfg = info.users; in - { - options.crans.users = { - ldap = { - enable = mkEnableOption "Authentification par le LDAP adm."; - }; - - root = { - passwordFile = mkOption { - type = types.path; - default = ../../secrets/common/root.age; - example = ../../secrets/apprentix/root.age; - description = "Fichier chiffré par age contenant le mot de passe root."; - }; - }; - }; + # Options : ../info/users.nix config = { age.secrets.root-passwd-hash = { @@ -39,20 +13,6 @@ in users = { mutableUsers = false; - motd = - let - shortRev = self.shortRev or self.dirtyShortRev or "inconnue"; - rev = self.rev or (lib.strings.removeSuffix "-dirty" (self.dirtyRev or "inconnue")); - isDirty = !(self ? rev); - in - '' - ${ - if isDirty then "!! DIRTY !! " else "" - }Version déployée ${shortRev}, modifiée le ${self.lastModifiedDate or "??"}. - - https://gitlab.crans.org/nounous/nixos/-/commit/${rev} - ''; - users.root = { hashedPasswordFile = config.age.secrets.root-passwd-hash.path; }; diff --git a/modules/crans/vlans.nix b/modules/crans/vlans.nix deleted file mode 100644 index c535aa8..0000000 --- a/modules/crans/vlans.nix +++ /dev/null @@ -1,230 +0,0 @@ -{ lib, ... }: -let - inherit (lib) - mkOption - types - mkDefault - mkForce - ; - - vlanConf = { - - srv = { - id = 2; - description = "Réseau public"; - interface = "ens19"; - ipv4 = { - address = mkForce "185.230.79.0"; - prefixLength = 26; - routes = [ - { - address = "0.0.0.0"; - via = "185.230.79.62"; - prefixLength = 0; - } - ]; - }; - ipv6 = { - address = mkForce "2a0c:700:10::"; - routes = [ - { - address = "::"; - via = "2a0c:700:2::ff:fe00:9902"; - prefixLength = 0; - } - ]; - }; - }; - - srv-nat = { - id = 3; - description = "Réseau derrière un NAT"; - interface = "ens19"; - ipv4.routes = [ - { - address = "0.0.0.0"; - via = "172.16.3.99"; - prefixLength = 0; - } - ]; - ipv6 = { - address = mkForce "2a0c:700:3::"; - routes = [ - { - address = "::"; - via = "2a0c:700:3::ff:fe00:9903"; - prefixLength = 0; - } - ]; - }; - }; - - san = { - id = 4; - description = "Accès aux baies de stockages"; - interface = "ens19"; - }; - - ceph = { - id = 6; - description = "Réseau interne à ceph"; - interface = "ens20"; - }; - - adm = { - id = 10; - description = "Réseau interne"; - interface = "ens18"; - }; - - adh = { - id = 12; - description = "Réseau adhérent"; - ipv4 = { - address = mkForce "185.230.78.0"; - routes = [ - { - address = "0.0.0.0"; - via = "185.230.78.12"; - prefixLength = 0; - } - ]; - }; - ipv6 = { - address = mkForce "2a0c:700:12::"; - prefixLength = 48; - routes = [ - { - address = "::"; - via = "2a0c:700:12::ff:fe00:9912"; - prefixLength = 0; - } - ]; - }; - }; - - adh-adm = { - id = 13; - description = "Réseau d’administration du réseau adhérent"; - }; - - ens = { - id = 2751; - description = "Interconnexion avec l’ENS."; - ipv4 = { - address = mkForce "138.231.136.0"; - prefixLength = 29; - }; - ipv6 = null; - }; - - ens-clubs = { - id = 2754; - description = "Réseau clubs interconnecté avec l’ENS"; - ipv6 = null; - }; - - ens-lp = { - id = 2756; - description = "Réseau imprimante interconnecté avec l’ENS"; - ipv6 = null; - }; - - }; - - ipOpt = - v: - let - maxPrefix = if v == 4 then 32 else 128; - in - types.submodule { - options = { - address = mkOption { - type = types.str; - description = "Adresses IPv${v} du réseau"; - }; - - prefixLength = mkOption { - type = types.ints.between 0 maxPrefix; - default = if v == 4 then 24 else 64; - description = "Prefixe du réseau"; - }; - - routes = mkOption { - default = [ ]; - description = "Routes par défaut"; - example = [ - { - address = "0.0.0.0"; - via = "172.16.3.99"; - prefixLength = 0; - } - ]; - type = types.listOf ( - types.submodule { - options = { - address = mkOption { type = types.str; }; - via = mkOption { type = types.str; }; - prefixLength = mkOption { type = types.ints.between 0 maxPrefix; }; - }; - } - ); - }; - }; - }; -in -{ - options.crans.vlans = mkOption { - type = types.attrsOf ( - types.submodule { - options = { - id = mkOption { - type = types.int; - example = "10"; - description = "Id du VLAN"; - }; - - description = mkOption { - type = types.str; - description = "Description du VLAN"; - }; - - interface = mkOption { - type = types.nullOr types.str; - default = null; - example = "ens19"; - description = "Interface par défaut du VLAN"; - }; - - ipv4 = mkOption { - type = types.nullOr (ipOpt 4); - description = "Réseau IPv4 du VLAN."; - }; - - ipv6 = mkOption { - type = types.nullOr (ipOpt 6); - description = "Réseau IPv6 du VLAN."; - }; - }; - } - ); - }; - - config.crans.vlans = ( - builtins.mapAttrs ( - vlan: conf: - let - vlan = lib.mod conf.id 100; - in - lib.mkMerge ( - [ conf ] - ++ lib.optional ((conf.ipv4 or true) != null) { - ipv4.address = mkDefault "172.16.${toString vlan}.0"; - } - ++ lib.optional ((conf.ipv6 or true) != null) { - ipv6.address = mkDefault "fd00:0:0:${toString vlan}::"; - } - ) - ) vlanConf - ); -} diff --git a/modules/info/acme.nix b/modules/info/acme.nix new file mode 100644 index 0000000..349f677 --- /dev/null +++ b/modules/info/acme.nix @@ -0,0 +1,7 @@ +{ lib, ... }: +let + inherit (lib) mkEnableOption; +in +{ + options.services.acme.enable = mkEnableOption "Activer les certificats ACME via let's encrypt."; +} diff --git a/modules/info/default.nix b/modules/info/default.nix new file mode 100644 index 0000000..e66e9d0 --- /dev/null +++ b/modules/info/default.nix @@ -0,0 +1,43 @@ +{ lib, config, ... }: +let + inherit (lib) + mkDefault + mkOption + mkIf + types + ; +in +# L’évaluation se fait en deux passes. +# 1. La flake.nix avec la lib/info.nix évalue les info.nix +# 2. Les VMs évaluent la conf en partant de cette évaluation en paramètre +# (anisi que celles des autres machines) +{ + imports = [ + ./acme.nix + ./home.nix + ./meta.nix + ./monitoring.nix + ./networking.nix + ./resticClient.nix + ./users.nix + ]; + options.enable = mkOption { + type = types.bool; + example = true; + description = "Activer la configuration Crans par défaut"; + # On n’utilise pas mkEnableOption pour forcer la définition de enable à true + # ou false (et limiter les oublis) + }; + config = mkIf config.enable { + homeNounou.enable = mkDefault true; + monitoring.enable = mkDefault true; + networking = { + enable = mkDefault true; + adm.enable = mkDefault true; + }; + resticClient.enable = mkDefault true; + users = { + ldap.enable = mkDefault true; + }; + }; +} diff --git a/modules/info/home.nix b/modules/info/home.nix new file mode 100644 index 0000000..cf3816c --- /dev/null +++ b/modules/info/home.nix @@ -0,0 +1,10 @@ +{ lib, ... }: +let + inherit (lib) mkEnableOption; +in +{ + options = { + homeNounou.enable = mkEnableOption "Monter /home_nounou."; + homeAdh.enable = mkEnableOption "Monter /home-adh."; + }; +} diff --git a/modules/info/meta.nix b/modules/info/meta.nix new file mode 100644 index 0000000..ef5cab8 --- /dev/null +++ b/modules/info/meta.nix @@ -0,0 +1,43 @@ +{ lib, ... }: +let + inherit (lib) mkOption types; +in +{ + options = { + name = mkOption { + type = types.str; + description = "Nom de la machine."; + }; + + description = mkOption { + type = types.str; + description = "Description de la machine."; + }; + + id = mkOption { + type = types.int; + example = 135; + description = " + ID de la machine : + - < 100 machine physique + - >= 100 VM ( id de la VM dans proxmox ) + "; + }; + + isVm = mkOption { + type = types.bool; + description = "Est-ce une machine virtuelle ?"; + }; + + isDebian = mkOption { + type = types.bool; + description = "Est-ce que c’est une machine sous Debian ?"; + default = false; + }; + + public-ssh = mkOption { + type = types.str; + description = "Clé ssh publique de la machine."; + }; + }; +} diff --git a/modules/info/monitoring.nix b/modules/info/monitoring.nix new file mode 100644 index 0000000..181abb5 --- /dev/null +++ b/modules/info/monitoring.nix @@ -0,0 +1,18 @@ +{ lib, ... }: +let + inherit (lib) mkEnableOption mkOption types; +in +{ + options.monitoring = { + enable = mkEnableOption "Monitoring prometheus de la machine."; + + nginx = { + enable = mkOption { + type = types.bool; + example = true; + description = "Monitoring de Nginx par prometheus."; + default = false; + }; + }; + }; +} diff --git a/modules/info/networking.nix b/modules/info/networking.nix new file mode 100644 index 0000000..1bec29c --- /dev/null +++ b/modules/info/networking.nix @@ -0,0 +1,148 @@ +{ + config, + infra, + lib, + ... +}: +let + inherit (lib) + fixedWidthString + hasPrefix + listToAttrs + mapAttrs + mergeAttrsList + mod + mkEnableOption + mkOption + optional + optionalAttrs + nameValuePair + types + ; + + switchOptions = listToAttrs ( + map ( + n: + nameValuePair n (mkOption { + type = types.nullOr types.int; + description = "Port sur le switch ${n}"; + default = null; + }) + ) infra.switchs + ); + + routesType = types.listOf ( + types.submodule { + options = { + address = mkOption { + type = types.str; + description = "Réseau de destination"; + }; + prefixLength = mkOption { + type = types.int; + description = "Masque du réseau de destination"; + }; + via = mkOption { + type = types.str; + description = "Ip de la gateway"; + }; + }; + } + ); +in +{ + options = { + ilo.id = mkOption { + type = types.nullOr types.int; + description = "Identifiant de l’ilo"; + default = null; + }; + ilo.switch = switchOptions; + + # TODO: vérifier que les IPs déclarées sont bien dans le réseau vlan (masque) + networking = { + enable = mkEnableOption "Activation du réseau Crans"; + switch = switchOptions; + } + // mapAttrs ( + vname: vconf: + mergeAttrsList ( + [ + { + enable = mkEnableOption "Activation du réseau ${vname}"; + + interface = mkOption { + type = types.str; + description = "Interface pour le réseau ${vname}"; + default = vconf.interface; + }; + + tag = mkOption { + type = types.int; + description = "Tag du vlan"; + readOnly = true; + default = vconf.id; + }; + } + ] + ++ optional ((vconf.ipv4 or { }) != null) { + ipv4.address = + mkOption { + type = types.str; + description = "Adresse IPv4 de la machine sur le réseau ${vname}"; + } + // (optionalAttrs (hasPrefix "172.16" (vconf.ipv4.address or "172.16.0.0")) { + default = "172.16.${toString vconf.id}.${toString config.id}"; + }); + + ipv4.prefixLength = mkOption { + type = types.int; + description = "Masque IPv4 de la machine sur le réseau ${vname}"; + default = vconf.ipv4.prefixLength or 24; + }; + + ipv4.routes = mkOption { + type = routesType; + description = "Routes de l’interface"; + default = vconf.ipv4.routes or [ ]; + }; + } + ++ optional ((vconf.ipv6 or { }) != null) ( + let + # XXX: Utilisation du masque pour le déterminer à la place ? + prefix = + if !(vconf ? ipv6) || lib.hasPrefix "fd00:0:0" vconf.ipv6.address then + "fd00:0:0" + else if lib.hasPrefix "2a0c:700" vconf.ipv6.address then + "2a0c:700" + else + (builtins.warn "Le prefixe de ${vconf.ipv6.address} n’est pas reconnu"); + in + { + ipv6.address = mkOption { + type = types.str; + description = "Adresse IPv6 de la machine sur le réseau ${vname}"; + default = "${prefix}:${toString vconf.id}::ff:fe0${toString (config.id / 100)}:${toString (mod config.id 100)}${ + fixedWidthString 2 "0" (toString vconf.id) + }"; + }; + + ipv6.prefixLength = mkOption { + type = types.int; + description = "Masque IPv6 de la machine sur le réseau ${vname}"; + default = vconf.ipv6.prefixLength or 64; + }; + + ipv6.routes = mkOption { + type = routesType; + description = "Routes de l’interface"; + default = vconf.ipv6.routes or [ ]; + }; + } + ) + ) + ) infra.vlans + + ; + }; +} diff --git a/modules/info/resticClient.nix b/modules/info/resticClient.nix new file mode 100644 index 0000000..252f44d --- /dev/null +++ b/modules/info/resticClient.nix @@ -0,0 +1,29 @@ +{ lib, ... }: +let + inherit (lib) mkEnableOption mkOption types; +in +{ + options.resticClient = { + enable = mkEnableOption "Configuration générale pour le client restic."; + + additionalPaths = mkOption { + type = types.listOf types.path; + default = [ ]; + example = [ "/backup" ]; + description = "Chemins à backuper en plus de ceux par défaut."; + }; + + additionalExcludes = mkOption { + type = types.listOf types.path; + default = [ ]; + example = [ "/var/lib//cache" ]; + description = "Chemins à exclure des backups en plus de ceux par défaut."; + }; + + when = mkOption { + type = types.str; + example = "05:42"; + description = "À quelle heure faire les backups."; + }; + }; +} diff --git a/modules/info/users.nix b/modules/info/users.nix new file mode 100644 index 0000000..d6d0e8b --- /dev/null +++ b/modules/info/users.nix @@ -0,0 +1,20 @@ +{ lib, ... }: +let + inherit (lib) mkEnableOption mkOption types; +in +{ + options.users = { + ldap = { + enable = mkEnableOption "Authentification par le LDAP adm."; + }; + + root = { + passwordFile = mkOption { + type = types.path; + default = ../../secrets/common/root.age; + example = ../../secrets/apprentix/root.age; + description = "Fichier chiffré par agenix contenant le mot de passe root."; + }; + }; + }; +} diff --git a/modules/services/acme.nix b/modules/services/acme.nix index f08bd9a..d9f6eb6 100644 --- a/modules/services/acme.nix +++ b/modules/services/acme.nix @@ -1,16 +1,18 @@ -{ config, lib, ... }: +{ + config, + info, + lib, + ... +}: let - cfg = config.crans.services.acme; + cfg = info.services.acme; - inherit (lib) mkEnableOption mkIf; + inherit (lib) mkIf; in { - options.crans.services.acme = { - enable = mkEnableOption "Activer les certificats ACME via let's encrypt."; - }; - + # Options : ../info/acme.nix config = mkIf cfg.enable { age.secrets = { acme-env.file = ../../secrets/acme/env.age; diff --git a/modules/services/nginx.nix b/modules/services/nginx.nix index b5f1c87..c4e61e7 100644 --- a/modules/services/nginx.nix +++ b/modules/services/nginx.nix @@ -1,5 +1,13 @@ -{ lib, config, ... }: +{ + lib, + config, + info, + ... +}: +let + cfg = info.monitoring; +in { services.nginx = lib.mkIf config.services.nginx.enable { recommendedProxySettings = true; @@ -15,7 +23,7 @@ real_ip_header X-Real-Ip; ''; - virtualHosts.nginx-stub-status = lib.mkIf config.crans.monitoring.enable { + virtualHosts.nginx-stub-status = lib.mkIf (cfg.enable && cfg.nginx.enable) { listen = [ { addr = "[::1]";