Skip to content

JulesdeCube/avalanche

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Avalanche

Avalanche is a nix flake library to generate complex NixOS configuration.

Inspiration

This project is inspire by Ansible the defunct NixOps project.

Getting started

Instalation

You can use Avalanche by incuding it in your falke input and laoding it's library.

{
  inputs = {
    avalanche.url = "git+https://gitlab.julesdecube.com/infra/avalanche.git";
    ...
  };

  output = { avalanche, ... }:
    let
      # Import the avalanche library.
      inherit (avalanche) lib;
    in {
      ...
    };
}

Usage

The main library function is mkInventory it generate Nixos System like the nixpkgs.lib.nixosSystem function but with the adition of groups:

lib.mkInventory {
  defaultModules = [];
  groups = { };
  hosts = { };
}

Example

following a flake.nix to deploy a loadbalanced dns server:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos";
    avalanche.url = "git+https://gitlab.julesdecube.com/infra/avalanche.git";
  };

  outputs = { nixpkgs, avalanche, ... }:
    let
      getIP = system: (builtins.elemAt system.config.networking.interfaces.eno1.ipv4.addresses 0).address;
    in
    {
      nixosConfigurations = avalanche.lib.mkInventory {
        defaultModules = [
          { nixpkgs.hostPlatform = "x86_64-linux"; }
        ];

        groups = {
          dns = { pkgs, hosts, ... }: {
            services.bind = {
              enable = true;
              zones."example.com" = {
                master = true;
                file = pkgs.writeText "zone-example.com" ''
                  $ORIGIN example.com.
                  @            IN      SOA     ns1 hostmaster ( 1 3h 1h 1w 1h)
                               IN      NS      ns1
                  ns1          IN      A       ${getIP hosts.lb01}
                '';
              };
            };
          };
        };

        hosts = {
          ns01 = { groups, ... }: {
            groups = [ groups.dns ];
            networking.interfaces.eno1.ipv4.addresses = [{ address = "10.0.0.9"; prefixLength = 24; }];
          };
          ns02 = { groups, ... }: {
            groups = [ groups.dns ];
            networking.interfaces.eno1.ipv4.addresses = [{ address = "10.0.0.8"; prefixLength = 24; }];
          };

          lb01 = { groups, groupsMembers, ... }: {
            networking.interfaces.eno1.ipv4.addresses = [{ address = "10.0.0.1"; prefixLength = 24; }];
            services.dnsdist =
              let
                mkServer = name: system: ''
                  newServer({address=${getIP system}, name="${name}"})
                '';
                servers = builtins.attrValues (builtins.mapAttrs mkServer groupsMembers.dns);
              in
              {
                enable = true;
                listenPort = 53;
                extraConfig = ''
                  setServerPolicy(roundrobin)
                  ${nixpkgs.lib.concatStrings servers}
                '';
              };
          };
        };
      };
    };
}

You can found more example in the example folder.

Documentation

You can found the project documentation inside the docs folder

About

Nix library to generate multi host deployement.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published