# Writing custom modules for nixos configuration

**URL:** https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652
**Category:** Help
**Created:** [August 18, 2026, 8:55am UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652 "2026-08-18T08:55:15Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![kazimierzkrauze](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@kazimierzkrauze](https://discourse.nixos.org/u/kazimierzkrauze)
#### Post date: [August 18, 2026, 8:55am UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/1 "2026-08-18T08:55:16Z")

</div>

Hi!

What is the preferred way of declaring custom options in nixos configuration? I tried with this layout:

```auto
modules/
├── default.nix
├── home-manager
│ ├── user0
│ │ ├── default.nix
│ │ └── packages
│ │ ├── graphical
│ │ │ └── default.nix
│ │ └── terminal
│ │ └── default.nix
│ └── user1
│ ├── default.nix
│ └── packages
│ ├── graphical
│ │ └── default.nix
│ └── terminal
│ └── default.nix
├── packages
│ ├── base
│ │ └── default.nix
│ ├── graphical
│ │ └── default.nix
│ └── terminal
│ └── default.nix
├── system
│ └── base
│ └── default.nix
└── users
    ├── groups
    │ └── group0
    │ └── default.nix
    ├── user0
    │ └── default.nix
    └── user1
        └── default.nix

```

And found out that it is cumbersome (difficult to read). Is there some more elegant way of doing this?

Also I thought of importing all of these modules using this file `modules/default.nix`:

```auto
{ config, ... }:
{
  imports = [
    ./home-manager/user1/default.nix
    ./home-manager/user1/packages/graphical/default.nix
    ./home-manager/user1/packages/terminal/default.nix
    ./packages/base/default.nix
    ./packages/graphical/default.nix
    ./packages/terminal/default.nix
    ./system/base/default.nix
  ];
}

```

This way I can import just one file to access all of my options. Is this good practice?

Best,  
Miro

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [August 18, 2026, 5:24pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/2 "2026-08-18T17:24:05Z")

</div>

Assuming those are all NixOS modules, sure. If they’re home-manager modules that will of course not work.

There is no universally preferred way, in any case.

---

<div class="post-metadata">

### Author: ![hugosenari](https://discourse.nixos.org/user_avatar/discourse.nixos.org/hugosenari/32/5630_2.png) [@hugosenari](https://discourse.nixos.org/u/hugosenari)
#### Post date: [August 18, 2026, 5:32pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/3 "2026-08-18T17:32:01Z")

</div>

> [@kazimierzkrauze](#):
>
> Preferred way

Is a subjective mattter 🙂

> [@kazimierzkrauze](#):
>
> more elegant way

Yes, rename files to tailcoat\_suit.nix

Did you know that you can import default.nix by dirname?  
imports = [./system/base];

> [@kazimierzkrauze](#):
>
> declaring custom options

I think, most people don’t create custom options, but custom configs.  
The difference is that “options” you create alternative to others “config” and isn’t hard, is fun, is cool, but most users don’t create them.

> [@kazimierzkrauze](#):
>
> Is this good practice?

Yes

> [@kazimierzkrauze](#):
>
> I tried with this layout

Nice layout, but is important focus on your use case:  
How many user you have, how many packages you have, how many machines you have?  
How much do you think you will need to change them?

For most users a single configuration.nix is enough.

See also:

> [@How do you structure your NixOS configs?](https://discourse.nixos.org/t/how-do-you-structure-your-nixos-configs/65851):
>
> Hey everyone! I’ve been on NixOS for a few months now and I’m trying to figure out the best way to organize my config. I want something that’s simple to understand but can grow with my setup. So far I’ve landed on this structure: ├── flake.nix ├── core/ # System-wide common │ ├── boot.nix │ ├── fonts.nix │ ├── packages.nix │ └── ... ├── home/ # Home Manager common │ ├── hyprland/ │ ├── scripts/ │ ├── firefox.nix │ ├── nvf.nix │ └── ... └─…

---

<div class="post-metadata">

### Author: ![Frontear](https://discourse.nixos.org/user_avatar/discourse.nixos.org/frontear/32/27055_2.png) [@Frontear](https://discourse.nixos.org/u/Frontear)
#### Post date: [August 18, 2026, 6:13pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/4 "2026-08-18T18:13:24Z")

</div>

There’s no one way to do it. I personally place my modules into `modules/nixos` or `modules/home-manager` depending on which level the module is for, and then I use the config attrset path to declare the directory, so if I was making a module called `my.foo` for NixOS, I’d place it at `modules/nixos/my/foo/module.nix`.

This works well enough for me, but it’s my unique preference. Others may have something similar, or something wildly different. Do what works for you. It should be noted that I’ve had different organization methods before settling on this, so maybe it’ll take some experimenting for you too.

---

<div class="post-metadata">

### Author: ![drupol](https://discourse.nixos.org/user_avatar/discourse.nixos.org/drupol/32/9471_2.png) [@drupol](https://discourse.nixos.org/u/drupol)
#### Post date: [August 18, 2026, 6:43pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/5 "2026-08-18T18:43:08Z")

</div>

Hello,

Perhaps you should have a look at the Den “framework” for managing your config ?

URL: [https://den.denful.dev/](https://den.denful.dev/)

It is basically a context-aware [aspect-oriented](https://en.wikipedia.org/wiki/Aspect-oriented_programming) Nix flake framework.

Find an example of how I manage my infra config at [GitHub - drupol/infra: Contains the configuration of every home computers · GitHub](https://github.com/drupol/infra)

Hope you’ll find what you’re looking for !

---

<div class="post-metadata">

### Author: ![kazimierzkrauze](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@kazimierzkrauze](https://discourse.nixos.org/u/kazimierzkrauze)
#### Post date: [August 18, 2026, 7:11pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/6 "2026-08-18T19:11:21Z")

</div>

> Assuming those are all NixOS modules, sure. If they’re home-manager modules that will of course not work.

I’ve managed even home-manager to work in this way:

```auto
f user0@kaktus 0s nixos-configuration main -> cat modules/home-manager/user0/default.nix
{ config, lib, ... }:
let
  cfg = config.internal.home-manager.user0.enable;
in
{
  options.internal.home-manager.user0 = {
    enable = lib.mkEnableOption "enable home-manager for user0";
  };
  config = lib.mkIf cfg {
    home-manager.users.user0 = { pkgs, ... }: {
      home = {
        stateVersion = "24.11";
        username = "user0";
        homeDirectory = "/home/user0";
      };
    };
  };
}
f user0@kaktus 0s nixos-configuration main ->

```

Is this correct / good practice?

---

<div class="post-metadata">

### Author: ![kazimierzkrauze](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@kazimierzkrauze](https://discourse.nixos.org/u/kazimierzkrauze)
#### Post date: [August 18, 2026, 7:20pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/7 "2026-08-18T19:20:23Z")

</div>

> [@hugosenari](#):
>
> Yes, rename files to tailcoat\_suit.nix

I’m sorry I don’t understand. English isn’t my first language so maybe there’s something I don’t know?

> [@hugosenari](#):
>
> Did you know that you can import default.nix by dirname?

I know now! Thank you.

> [@hugosenari](#):
>
> Nice layout, but is important focus on your use case:  
> How many user you have, how many packages you have, how many machines you have?  
> How much do you think you will need to change them?

Three users, 6ish machines and I’m a tinkerer, so… very much:)

---

<div class="post-metadata">

### Author: ![kazimierzkrauze](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@kazimierzkrauze](https://discourse.nixos.org/u/kazimierzkrauze)
#### Post date: [August 18, 2026, 7:22pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/8 "2026-08-18T19:22:41Z")

</div>

Thank you for sharing. Could you show me an example of this “config attrset path”? As I understand correctly it is a concept new to me:)

---

<div class="post-metadata">

### Author: ![kazimierzkrauze](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/k/898d66/32.png) [@kazimierzkrauze](https://discourse.nixos.org/u/kazimierzkrauze)
#### Post date: [August 18, 2026, 7:24pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/9 "2026-08-18T19:24:53Z")

</div>

I’ve heard about it but couldn’t find any good documentation. I’ll look into it!

---

<div class="post-metadata">

### Author: ![drupol](https://discourse.nixos.org/user_avatar/discourse.nixos.org/drupol/32/9471_2.png) [@drupol](https://discourse.nixos.org/u/drupol)
#### Post date: [August 18, 2026, 7:52pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/10 "2026-08-18T19:52:11Z")

</div>

Feel free to join the friendly Matrix room to get you started !

[https://matrix.to/#/#denful:matrix.org](https://matrix.to/#/#denful:matrix.org)

---

<div class="post-metadata">

### Author: ![waffle8946](https://discourse.nixos.org/user_avatar/discourse.nixos.org/waffle8946/32/27221_2.png) [@waffle8946](https://discourse.nixos.org/u/waffle8946)
#### Post date: [August 18, 2026, 11:35pm UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/11 "2026-08-18T23:35:24Z")

</div>

That’s a NixOS module, not a home-manager one 😉

---

<div class="post-metadata">

### Author: ![ghb](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/g/b2d939/32.png) [@ghb](https://discourse.nixos.org/u/ghb)
#### Post date: [August 20, 2026, 9:38am UTC](https://discourse.nixos.org/t/writing-custom-modules-for-nixos-configuration/79652/12 "2026-08-20T09:38:30Z")

</div>

Initially, when I setup my desktop I thought about taking the same approach as you, but I too found it _aesthetically unpleasing_. I wanted, almost the same layout on my Desktop as on my Laptop, but I also wanted the possibility of add one program to one machine without adding it to the other. To achieve this, I decided to base my setup upon a set of _generic_ [NixOS modules](https://wiki.nixos.org/wiki/NixOS_modules), somewhat inspired by [this thread about multi-system flake configurations](https://discourse.nixos.org/t/advice-on-multi-system-flake-configuration/74518).

```txt
├── common/ # All common components across machines
│ ├── default-configuration.nix
│ ├── default-home.nix
│ ├── desktop-environments
│ │ ├── cosmic.nix
│ │ └── kde.nix
│ └── vim.nix
├── flake.lock
├── flake.nix
└── hosts # Machine specific configurations
    ├── desktop
    │ ├── configuration.nix
    │ └── hardware-configuration.nix
    └── t480
        ├── configuration.nix
        └── hardware-configuration.nix

```

To reduce the need for repeating myself, I defined options, for the NixOS module `default-configuration.nix` to hold the most commonly used values. Below, an excerpt of the aforementioned file, full contents [on my GitHub](https://github.com/GustavGroenborg/.nixos/blob/5dd6d02fbb93e426fe250701db27726b970d1a0e/common/default-configuration.nix).

```nix
# Please read the docs about stateVersion. Link is in the bottom.
{ config, lib, pkgs, ... }:

with lib;

{
  imports = [
    ./vim.nix
  ];
  
  options.custom.identity = {
    username = mkOption {
      type = types.str;
      description = "Name of the user account.";
    };
    hostname = mkOption {
      type = types.str;
      description = "The network hostname of the machine.";
    };
    stateVersion = mkOption {
      type = types.str;
      description = "The first version of NixOS installed on this machine. Read the docs: https://nixos.org/nixos/options.html";
    };
  };

  config = {
    environment.systemPackages = with pkgs; [
      curl
      wget
    ];

    virtualisation.podman = {
      enable = true;
      dockerCompat = true;
    };

    networking.hostName = config.custom.identity.hostname;

   # ... omitted code ...
   
   # Define a user account. Don't forget to set a password with ‘passwd’.
    users.users."${config.custom.identity.username}" = {
      isNormalUser = true;
      description = "Gustav Hjort Bonnerup";
      extraGroups = ["networkmanager" "wheel"];
    };

    # ... omitted code ...

    nix.settings.experimental-features = ["nix-command" "flakes"];
    nix.settings.trusted-users = ["root" config.custom.identity.username];

    # ... omitted code ...

    # Before changing this value read the documentation for this option
    # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
    system.stateVersion = config.custom.identity.stateVersion; # Did you read the comment?
  };
}

```

For my default home manager setup, I likewise define a NixOS module, however in the shorthand format instead, as I pull in the above defined default values from the environment. Again, below is an excerpt, the full contents is available [on my GitHub](https://github.com/GustavGroenborg/.nixos/blob/5dd6d02fbb93e426fe250701db27726b970d1a0e/common/default-home.nix).

```nix
{ config, lib, osConfig, pkgs, pkgs-unstable, ...}:

with lib;

{
  # ... omitted code ...
  
  home.username = osConfig.custom.identity.username;
  home.homeDirectory = "/home/${osConfig.custom.identity.username}";
  home.stateVersion = osConfig.custom.identity.stateVersion;
  home.packages = with pkgs; [
    bc
    devenv # Consider deleting at some point
    htop
    just
    kdePackages.okular
    libreoffice
    nixd
    ripgrep
    texliveFull
    tree

    pkgs-unstable.antigravity-cli
  ];

  programs.direnv = {
    enable = true;
    nix-direnv.enable = true;
  };

  programs.emacs = {
    enable = true;
    package = pkgs.emacs;
  };

  programs.firefox.enable = true;

  programs.git = {
    enable = true;
    settings.user = {
      name = "Gustav Hjort Bonnerup";
      email = "gustav@hjortbonnerup.dk";
    };

    ignores = [
      "*~"
      "*.swp"
      ".direnv/"
      ".envrc.local"
    ];
  };

  # ... omitted code ...

}

```

Lastly, when configuring a specific machine, I pull in the default configurations shown above, into a machine specific configuration, e.g. `hosts/desktop/configuration.nix`, and on top of that add additional configurations for that specific machine. Below, the full contents of my desktop configuration, please not that I have added some clarifying comments, prefixed with `NOTE:`, that are not in the actual config.

```nix
let # NOTE: Global values defined below
  username = "ghb";
  hostname = "desktop";
  stateVersion = "26.05";
in
{ config, pkgs, ... }: {
    
  imports = [ # NOTE: Importing NixOS modules here
    ./hardware-configuration.nix
    ../../common/default-configuration.nix
    ../../common/desktop-environments/kde.nix
  ];

  # Passing variables to modules imported above
  custom.identity = {
    inherit username hostname stateVersion;
  };

  boot.kernelModules = [
    "nct6683" # Driver for motherboard fans
  ];
  
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  hardware.graphics.enable = true;
  hardware.graphics.enable32Bit = true;

  programs.coolercontrol.enable = true;
  
  # NOTE: Configuring home manager setup
  home-manager.users."${username}" = {
    imports = [
      ../../common/default-home.nix
      {
        home.packages = with pkgs; [
          steam
        ];
      }
    ];
  };

}

```

If considering multiple users on the same machine, the home manager configuration can easily be configured to not, import the values from the global environment, but instead using the values, that are passed to the NixOS module. This can be done by **not** using the NixOS module shorthand format, as is done for `common/default-configuration.nix`.

If it can help as inspiration, my [entire NixOS setup is available on my GitHub](https://github.com/GustavGroenborg/.nixos/tree/9eb1ef4a441cbdf36781cda85ce44b4953d46802).
