snowfallorg / lib

Unified configuration for systems, packages, modules, shells, templates, and more with Nix Flakes.
https://snowfall.org
Other
346 stars 28 forks source link

How can I create an module to use in others flakes? #79

Open damidoug opened 2 months ago

damidoug commented 2 months ago

Snowfall.lib is the best nixos library ever, but i'm noob in flakes and I'm trying to make an module to use in others flakes, this is my example flake.nix

{
  description = "nixos hardware library";

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

    snowfall-lib = {
      url = "github:snowfallorg/lib";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs:
    inputs.snowfall-lib.mkFlake {
      inherit inputs;

      src = ./.;

      snowfall = {
        namespace = "nixos-hardware";
      };

      channels-config.allowUnfree = true;

      nixosModules.nixos-hardware = import ./modules/nixos;
    };
}

in my package i have the modules/nixos folder and inside i have the options like audio/default.nix etc... see one of default.nix file example.

{
  options,
  config,
  lib,
  pkgs,
  ...
}:
with lib;
with lib.nixos-hardware; let
  cfg = config.nixos-hardware.audio;
in {
  options.nixos-hardware.audio = with types; {
    enable = mkBoolOpt false "Enable module";
  };

  config = mkIf cfg.enable {
    # Disable PulseAudio
    hardware.pulseaudio.enable = mkForce false;
    # Set it to false, as sound.enable is only meant for ALSA-based configurations
    sound.enable = mkForce false;
    # rtkit is optional but recommended
    security.rtkit.enable = mkDefault true;
    # Enable sound with pipewire.
    services.pipewire = {
      enable = true;
      alsa.enable = true;
      alsa.support32Bit = true;
      pulse.enable = true;
      jack.enable = true;
      wireplumber.enable = true;
    };
  };
}

now i want to make this my nixos-hardware available to import in other flake like

nixos-hardware.url = "URL";
systems.modules.nixos = with inputs; [
  nixos-hardware.nixosModules.nixos-hardware;
];

and use like

nixos-hardware = {
    boot = enabled;
    audio = enabled;
    network = enabled;
    cpu.intel = enabled;
    gpu.nvidia = enabled;
    disk.ssd = enabled;
  };

how can I do it? thanks.

jakehamilton commented 2 months ago

Hey there! If you're using Snowfall Lib you don't need to export nixosModules manually, the library does all of that for you. If you want to consume your flake from another one later then you can add it as an input and include the module just like you've posted here. Do you have a specific problem you're running into? What have you tried so far?

damidoug commented 1 month ago

Sorry for the delay, that's working.

damidoug commented 1 month ago

Sorry for re-open, but i did an change in my code and I cant make it work, this is my repo to you check: https://github.com/damianodoug/better-nixos-hardware

I I want to use in other flake like:

nixos-hardware = { audio = enabled; network = enabled; cpu.intel = enabled; gpu.nvidia = enabled; ssd = enabled; },,,,

when I try to import always have errors, don't find etc... please give me an help with this, feel free to fix my code, and show me how can I import without errors. thanks.

pinkcreeper100 commented 1 month ago

well you have snowfall.root set to ./nix which doesn't exist https://github.com/damianodoug/better-nixos-hardware/blob/main/flake.nix#L20

damidoug commented 1 month ago

Fixed, but the error still.

 error: attribute 'default' missing

       at /nix/store/ks7p2hwhimyabbgj5ry9bi66alykxz7s-source/flake.nix:76:9:

           75|         impermanence.nixosModules.impermanence
           76|         nixos-hardware.nixosModules.default

don't meter if I set

systems.modules.nixos = with inputs; [
        nixos-hardware.nixosModules.default or nixos-hardware.nixosModules.nixos-hardware
 ];
brenix commented 1 week ago

Experiencing a similar issue - I have a flake with a snowfall namespace of "matrix". Then, I want to include this as an input in a private flake and enable various options

Example home-manager config:

{
  matrix = {
    programs.terminal.tools.git.enable = true;
  }
}

But the result is

error: The option `matrix' does not exist

When looking at the flake outputs for the flake, I see the exported nixosModules, but darwinModules, homeModules, lib, and others show a status of unknown.

In the private flake, I've tried including it like so without success:

systems.modules.darwin = with inputs; [
  matrix.darwinModules # as well as matrix.darwinModules."programs/terminal/tools/git"
];

homes.modules = with inputs; [
  matrix.homeModules
];

Perhaps I'm just not making sense of how to export or reference the modules, but it would be nice if this were possible.

pinkcreeper100 commented 1 week ago

@brenix darwinModules, homeModules and nixosModules should each have .default (or the output name) on the end when importing them

as in your example:

systems.modules.darwin = with inputs; [
  matrix.darwinModules.default
];

homes.modules = with inputs; [
  matrix.homeModules.default
];
pinkcreeper100 commented 1 week ago

When looking at the flake outputs for the flake, I see the exported nixosModules, but darwinModules, homeModules, lib, and others show a status of unknown.

this is because nix is unable to understand some of these output types, however it is able to evaluate them

brenix commented 1 week ago

@brenix darwinModules, homeModules and nixosModules should each have .default (or the output name) on the end when importing them

as in your example:

systems.modules.darwin = with inputs; [
  matrix.darwinModules.default
];

homes.modules = with inputs; [
  matrix.homeModules.default
];

@pinkcreeper100 I tried this too, but end up seeing something along the lines of:

error: attribute 'default' missing
at /nix/store/ddqgwxavciip90zmc6aab7xx6668prn8-source/flake.nix:56:9:
  55|       homes.modules = with inputs; [
  56|         matrix.homeModules.default
pinkcreeper100 commented 1 week ago

and @damidoug, nixos-hardware doesn't provide default

you can see the modules it provides in the flake.nix

pinkcreeper100 commented 1 week ago

@brenix try setting an alias within matrix

https://snowfall.org/guides/lib/alias/

pinkcreeper100 commented 1 week ago

also it appears that if you import something like matrix.homeModules.theme.gtk it might work

ignore this

brenix commented 1 week ago

@pinkcreeper100 Thanks for helping out here - I unfortunately am still not able to achieve what I thought was possible here. Dropping into nix repl and running a = builtins.getFlake (toString ./.) and listing things, I am able to see all of my modules (darwin/home/nixos) and can confirm there is no default unless using an alias. However it seems this can only be used to target a specific path.

I even went as far as specifying every module individually under sytems.modules... and home.modules.. which appears to get me closer but seeing other errors now. One other thing I thought to do was create something like a modules/home/default.nix and use the snowfall lib to recursively import all files under that, then set that as the default alias. This doesn't seem like the right approach but I'm running out of ideas haha.

My hope was that I could simply include all modules with one line in my flake, then consume those modules. Will poke around a bit more.

pinkcreeper100 commented 1 week ago

@brenix mhm the only way i can see is doing it similar to snowflakeos

see the modules and installer template(s)