[Nix-dev] Best Practices on Modularizing Configuration.nix?

David Izquierdo thecofee at gmail.com
Mon Feb 27 17:31:30 CET 2017


My setup is also very similar. I have a configuration.nix with all the 
common setup, which imports hosts/current.nix, which is a (.gitignored) 
symlink to the appropiate host.nix. Then, the host.nix imports from 
modules/*.nix. I also have an etc with generic (not written in Nix) 
configuration files that get imported via builtins.readFile (shell RCs, 
for example) or referred to by *.configFile (i3 config).


I'm planning a slight refactor in which the symlink is actually 
configuration.nix, so that I can make certain reusable declarations into 
a module (I like specific users, UIDs, passwords, and hostnames for each 
host, right now there's a lot of identical lines, varying only the 
username).


I _do_ sync the config tree to my server, since it also serves as a 
general purpose remote computer, and I end up using it a lot when not on 
a machine I own.


On 27/02/17 15:28, Tomasz Czyż wrote:
> Hey Mark,
>
> I use almost the same setup and for 1.5y works very well.
>
> I have "modules" and "hosts" directories. Each "host" contains
> configuration about hardware/disk setup and includes set of modules from
> "modules". On each host there is a symlink to correct
> "hosts/<hostname>.nix" file.
>
> I found this setup pretty robust on dev machines.
> On servers I use nixops which pushes configuration so I don't manage
> /etc/nixos there.
>
> Tom
>
> 2017-02-27 14:15 GMT+00:00 Mark Gardner <mkg at vt.edu>:
>
>> Now that I am putting NixOS on more and more machines, I would like to
>> modularize and share parts of the config to maximize reuse and ensure
>> uniformity. My approach is to consider the sub-config files as traits or
>> roles and combine them together to create configuration.nix for a specific
>> machine, like this:
>>
>> - cfg/common.nix  # common config
>> - cfg/desktop.nix  # xorg and related
>> - cfg/laptop.nix  # related to all laptops
>> - cfg/work.nix  # work location related
>> ...
>> - cfg/mylaptop.nix  # specific laptop related
>>
>> I import from these to make up configuration.nix. For example, on my
>> laptop, configuration.nix contains:
>>
>> ---
>> { config, pkgs, ... }:
>>
>> {
>>    imports =
>>      [
>>        ./hardware-configuration.nix
>>        ./cfg/mylaptop.nix
>>        ./cfg/common.nix
>>        ./cfg/desktop.nix
>>        ./cfg/laptop.nix
>>        ./cfg/work.nix
>>      ];
>> }
>> ---
>>
>> So far, this seems like a good approach. Except that each machine has its
>> own configuration.nix that I would like to keep in the git repository too
>> but of course I can't have different top level files with the same name. To
>> solve this, I could moved the current configuration.nix inside of cfg (as
>> cfg/mylaptop.cfg.nix perhaps) or merge with the existing cfg/mylaptop.nix
>> then making configuration.nix a symlink to it. That way the only thing to
>> do by hand is create the symlink to select a particular configuration. Is
>> this reasonable? Is there a better way to do it?
>>
>> How do you modularize your configuration and put it into a repo such that
>> you can easily create a configuration for a new machine (and put it in the
>> repo too) without a lot of hand work?
>>
>> Mark
>> --
>> Mark Gardner
>> --
>>
>> _______________________________________________
>> nix-dev mailing list
>> nix-dev at lists.science.uu.nl
>> http://lists.science.uu.nl/mailman/listinfo/nix-dev
>>
>>
>
>
>
> _______________________________________________
> nix-dev mailing list
> nix-dev at lists.science.uu.nl
> http://lists.science.uu.nl/mailman/listinfo/nix-dev



More information about the nix-dev mailing list