Beginner needs help with NixOS configuration

Hi people, here’s my current config. It is very simple and I just started using flake. I have learnt the Nix language but the lack of documentation and extreme variations in example configs by the community has left me confused…
I wish to use home-manager or at least manage my config with symlinks created in my config.

So far, I understand that when I import modules, the expressions have access to the “global config” just like configuration.nix. One of the things that somewhat made sense was the following, but I felt like the identifier nixpkgs might have a similar use:

{ pkgs, ... }:
{
  environment.systemPackages = with pkgs; [
      # here be packages
  ]
}

I have tried to make a module that has a .enable property, and it involved passing the expression a config parameter which I genuinely got lost at trying to find resources for. I hoped using nil LSP would help me but it didnt help with the finding options problem. Can anyone help me build off of this simple config?

If you mean “module argument”, like pkgs, nixpkgs simply doesn’t exist.

The nix.dev docs dive into what’s going on here, but it’s quite the rabbit hole. I suggest simply using the arguments as you see them in other people’s configuration until you get an intuition for them (and/or become capable of reading nixpkgs source code).

config simply contains the global configuration. E.g. this would write the names of all installed packages to a file in /etc:

{ config, lib, ... }: {
  environment.etc."packages".text = lib.pipe config.environment.systemPackages [
    (map (p: p.name))
    lib.concatLines
  ];
}

All of this is unrelated to using home-manager, though, what is your actual question?

1 Like

Hi, thanks for the reply
I am comfortable with reading documentation, I’ve tried to rely on intuition but it hasn’t kicked in yet. I can also read nixpkgs source code, that is how I made neovim the default editor and enabled insults and pwfeedback in sudo. Doesnt the module automatically receive the global configuration? Whats the point of the config argument?

I need guidance on how I would go about including my program config files in my NixOS configuration and generally expanding it to use more Nix features. I just mentioned home-manager as something I tried but didnt understand.

For example, how would I make these modules have a .enable property, and say have it default to true, and so on?

You can write global configuration, yes, but to read it you need the config arg.

That’s what I’m trying to show in my snippet. If you want to make the value of one option depend on the value of another, you need config.

In the concrete case you’re asking for, you need to know if the module is enabled, for example:

{ config, lib, ... }: {
  options.somemodule.enable = lib.mkEnableOption "some module";

  # Note we're using the `config` variable here
  # to figure out whether the module should be
  # enabled.
  config = lib.mkIf config.somemodule.enable {
    # Whatever your module does
  };
}

… but the nix.dev docs do an infinitely better job at explaining this than I care to reproduce on discourse. Read the docs I linked.

All that said, for writing a personal NixOS configuration, you probably should not define any options at all, especially if you’re just getting started. They’re unnecessary boilerplate until you’re doing something much more advanced.

2 Likes

Understood. I was trying to shoehorn things like this in just as a learning exercise and I didn’t intend to use every feature for the sake of them existing. Thanks a lot for the help!

1 Like

Not trying to discourage learning, of course, but it’s probably worth waiting a little with this specific topic :wink:

2 Likes