Video_76.md

Watch the video

There is one mistake in the video, note that you have to inherit pkgs; when using wrapper-modules below!

Initialize a flake

$ nix --extra-experimental-features "nix-command flakes" flake init -t github:vimjoyer/flake-parts-wrapped-template

Available at https://github.com/vimjoyer/flake-parts-wrapped-template

wrapper modules: https://birdeehub.github.io/nix-wrapper-modules/md/intro.html

flake-parts: https://flake.parts/

my niri config: https://github.com/vimjoyer/nixconf/blob/main/modules/wrappedPrograms/niri.nix

Example Project Structure

.
├── flake.nix
└── modules
    ├── hosts
    │   └── myMachine
    │       ├── default.nix
    │       ├── configuration.nix
    │       └── hardware.nix
    └── features
        ├── niri.nix
        └── noctalia.nix

The Flake Entry Point (flake.nix)

We use import-tree to automatically load all modules within the modules/ directory.

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";

    flake-parts.url = "github:hercules-ci/flake-parts";
    import-tree.url = "github:vic/import-tree";

    wrapper-modules.url = "github:BirdeeHub/nix-wrapper-modules";
  };

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

Machine Definition (modules/hosts/myMachine/default.nix)

We use import-tree to automatically load all modules within the modules/ directory.

{ self, inputs, ... }: {
  flake.nixosConfigurations.myMachine = inputs.nixpkgs.lib.nixosSystem {
    modules = [
      self.nixosModules.myMachineConfiguration
    ];
  };
}

Machine configuration (modules/hosts/myMachine/configuration.nix)

Copy your original configuration from /etc/nixos/configuration.nix and paste it in myMachineConfiguration, and don’t forget to enable flakes with the nix.settings.experimental-features option.

Any nixos modules you create later can be added to your nixos configuration with imports.

{ self, inputs, ... }: {

  flake.nixosModules.myMachineConfiguration = { pkgs, lib, ... }: {
    # import any other modules from here
    imports = [
      self.nixosModules.myMachineHardware
      self.nixosModules.niri
    ];

    nix.settings.experimental-features = [ "nix-command" "flakes" ];
  
    # ...
  };

}

Niri (modules/features/niri.nix)

{ self, inputs, ... }: {
  flake.nixosModules.niri = { pkgs, lib, ... }: {
    programs.niri = {
      enable = true;
      package = self.packages.${pkgs.stdenv.hostPlatform.system}.myNiri;
    };
  };

  perSystem = { pkgs, lib, self', ... }: {
    packages.myNiri = inputs.wrapper-modules.wrappers.niri.wrap {
      inherit pkgs; # THIS PART IS VERY IMPORTAINT, I FORGOT IT IN THE VIDEO!!!
      settings = {
        spawn-at-startup = [
          (lib.getExe self'.packages.myNoctalia)
        ];

        xwayland-satellite.path = lib.getExe pkgs.xwayland-satellite;

        input.keyboard.xkb.layout = "us,ua";

        layout.gaps = 5;

        binds = {
          "Mod+Return".spawn-sh = lib.getExe pkgs.kitty;
          "Mod+Q".close-window = null;
          "Mod+S".spawn-sh = "${lib.getExe self'.packages.myNoctalia} ipc call launcher toggle";
        };
      };
    };
  };
}

Noctalia Shell (modules/features/noctalia.nix)

$ nix run nixpkgs#noctalia-shell ipc call state all > ./modules/features/noctalia.json
{ self, inputs, ... }: {
  perSystem = { pkgs, ... }: {
    packages.myNoctalia = inputs.wrapper-modules.wrappers.noctalia-shell.wrap {
      inherit pkgs; # THIS PART IS VERY IMPORTAINT, I FORGOT IT IN THE VIDEO!!!
      settings =
        (builtins.fromJSON
          (builtins.readFile ./noctalia.json)).settings;
    };
  };
}