example.nix.md

Flake-parts NixOS + home-manager example

This flake-parts example comes equipped with automatic module importing, one nixos system, one nixos module for it, one home-manager configuration and a home-manager module. The home-manager configuration is both standalone and used as a module in this setup.

A great start for a dendritic flake.

./flake.nix

Entry point of your flake

{
  description = "Nix flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-parts.url = "github:hercules-ci/flake-parts";
    import-tree.url = "github:vic/import-tree";
    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs: inputs.flake-parts.lib.mkFlake {inherit inputs;} (inputs.import-tree ./modules);
}

./modules/parts.nix

Module declaring systems supported by your flake, and importing a home-manager flake-parts module.

read more at:

https://flake.parts/options/home-manager.html

{ inputs, ... }: {
  imports = [
    # adds home-manager options to flake-parts
    inputs.home-manager.flakeModules.home-manager
  ];

  config.systems = [ 
    "x86_64-linux"
    "aarch64-linux"
    "x86_64-darwin"
    "aarch64-darwin"
  ];
}

./modules/hosts/myMachine/configuration.nix

Module outputting a nixos system and a configuration module for it.

{ self, inputs, ... }: {

  # This is your system configuration entry-point
  flake.nixosConfigurations.HOSTNAME = inputs.nixpkgs.lib.nixosSystem {
    modules = [
      self.nixosModules.HOSTNAMEModule
      self.nixosModules.myHomeManager
    ];
  };

  # This is your configuration.nix, a place where you configure your system
  # You can place it in a separate file.
  flake.nixosModules.HOSTNAMEModule = { pkgs, ... }: {
    environment.systemPackages = [
      pkgs.vim
      pkgs.firefox
    ];

    users.users.USERNAME = {
      isNormalUser = true;
      shell = pkgs.fish;
    };
    home-manager.users.USERNAME = self.homeModules.USERNAMEModule;
  };

}

./modules/features/home-manager.nix

Module adding home-manager NixOS module to the imports and configuring it.

{ self, inputs, ... }: {

  # This is your module that imports and configures home-manager
  flake.nixosModules.myHomeManager = { pkgs, ... }: {
    imports = [
      inputs.home-manager.nixosModules.default # import official home-manager NixOS module
    ];

    home-manager = {
      useGlobalPkgs = true;
      useUserPackages = true;
    };
  };

}

./modules/hosts/myMachine/home.nix

Module outputting a standalone home-manager configuration and a module for it. Module is also reused above in the configuration.

{ self, inputs, ... }: {

  # This is your standalone home-manager configuration, meant to be used on non-nixos machines
  # with the home-manager command
  flake.homeConfigurations.USERNAME = inputs.home-manager.lib.homeManagerConfiguration {
    pkgs = import inputs.nixpkgs { system = "x86_64-linux"; };
    modules = [
      self.homeModules.USERNAMEModule
      {
        home.username = "USERNAME";
        home.homeDirectory = "/home/USERNAME";
      }
    ];
  };

  # This is your home.nix, your module where you configure home-manager
  # It's imported both in standalone configuration above, and in your nixos configuration
  flake.homeModules.USERNAMEModule = { pkgs, ... }: {
    programs.bash.enable = true;
    programs.bash.shellAliases.ll = "ls -l";

    home.packages = [ pkgs.hello ];
    home.stateVersion = "24.11";
  };

}