I just want to know from you how you (or how you would) pass options to packages you develop with flakes.

I am currently writing a widget bar using Astal and then import the flake in my NixOS flake and use the package.default output in home.packages. I’d like to also style this bar according to my Stylix theme, how do you do something like this?

  • balsoft@lemmy.ml
    link
    fedilink
    English
    arrow-up
    4
    ·
    3 days ago

    I usually provide a NixOS/home-manager module together with my flake, that can then be imported and customized by setting the config options.

    • Yash Raj@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 days ago

      can you provide me with a simple example, and syntax. I have tried, but failed. I want that the astal config uses a custom colorscheme which i configure somehow that can be given as option when importing that flake in the main nixos system.

      • balsoft@lemmy.ml
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        2 days ago

        I’m afraid it’s difficult to provide a simple example as there are a bunch of moving parts.

        In essence, you create a file (e.g. module.nix) in your package repo:

        self:
        {
          config,
          pkgs,
          lib,
          ...
        }:
        let
          cfg = config.programs.my-package;
        in
        {
          # Here we define the options that we can tweak and configure.
          options = {
            programs.my-package = {
              # Whether to configure anything in the first place
              enable = lib.mkEnableOption "my package";
              # Which colors to use
              colors =
                let
                  # This is just a function to reduce boilerplates. It generates options from the name of the color setting
                  color =
                    name:
                    lib.mkOption {
                      description = "${name} color for my-package";
                      type = lib.types.str;
                    };
                in
                {
                  bg = color "Background";
                  fg = color "Foreground";
                  # other colors here too
                };
            };
            # Whether to use stylix to configure colors
            stylix.targets.my-package.enable = lib.mkEnableOption "Stylix configuration for my-package";
          };
          # Here we set up our package (if programs.my-package.enable is true)
          config = lib.mkIf cfg.enable {
            # Add our package to home.packages (you're doing this already, we're just moving it here for convenience)
            home.packages = [ self.packages.${pkgs.system}.my-package ];
            # Here is the part where we do the actual configuring. This depends on how your package reads its configuration.
            xdg.configFile."my-package/colors.toml".text = ''
              <somehow generate the config file using ${cfg.bg} and ${cfg.fg} etc
            '';
            # This is a bit recursive but should be fine. If if throws errors about infinite recursion then tell me :)
            # Here we set the default values for the colors in our configuration to whatever is provided via stylix (provided you enable the corresponding config setting)
            programs = lib.mkIf stylix.targets.my-package.enable {
              my-package.colors.bg = lib.mkDefault config.stylix.colors.base00;
              my-package.colors.fg = lib.mkDefault config.stylix.colors.base05;
              # etc
            };
          };
        }
        

        Then you make sure your program reads the colors from ~/.config/my-package/colors.toml (you can use whatever format or path you want, but adjust the module accordingly), and uses those colors

        Finally, you add homeModules.default = import ./module.nix self; to your package flake, and imports = [ inputs.my-package.homeModules.default ]; programs.my-package.enable = true; stylix.targets.my-package.enable = true; to your home-manager configuration. You will need to adjust a lot of stuff depending on how exactly you want to do this but this should get you started.

        All of this is a bit complicated but I think for a good reason; this setup is really quite flexible and will allow you to expand easily in the future should you continue work on the project. And if not, it gives you a chance to learn about a lot of different Nix concepts that will come in useful later :)

        • Yash Raj@lemmy.worldOP
          link
          fedilink
          English
          arrow-up
          2
          ·
          2 days ago

          Wow, my G! This is incredibly well-structured, and I really appreciate that you even included comments. It’s genuinely helpful, and I’m definitely going to give it a try.

          I actually posted this while my exams are ongoing, hoping someone would respond in the meantime. For real, thank you so much. I love this community!