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?

  • Yash Raj@lemmy.worldOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    13 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
      13 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
        ·
        13 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!

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

        @balsoft Help wanted my G!, I am stuck here I created this flake for my astal widget config: https://github.com/mobsenpai/astal.git

        And I have this flake for my main nixos config: https://github.com/hana.git

        I can try to import it now, but perhaps it seems that there is some error here. I try building this astal config using nix build (just to try to test it) and it creates a result folder inside the astal folder, where i have its flake and configs, see the astal repo, those are the contents of the folder. But there is no check.scss file gets created in the scss/theme/ folder. I haven’t yet tried to import it into my nixos system, for now, didn’t get the time, but will update you on that, but I feel something if not a lot of things are not good here.

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

          How are you importing it in your nixos config? I don’t see any references to astal in there.

          I try building this astal config using nix build (just to try to test it) and it creates a result folder inside the astal folder, where i have its flake and configs, see the astal repo, those are the contents of the folder. But there is no check.scss file gets created in the scss/theme/ folder.

          That sounds right, nix build on that flake by itself wouldn’t produce that file. You would have to import that home-manager module to your config and then switch to that config, and then the file will be created in your homedir, in ~/.config/astal/scss/themes/astel.scss to be specific.

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

            So I am importing in home manager file and the syntax and building is fine, the file is also being created in my ~/requested folder.

            But I think there has been a misunderstanding in my concept of how this package is being installed and where it will get its colors from, so this flake package astal takes colors from where it is built right? and inside there will be created a scss folder which will have the themes/astel.css file, but in our setup we will only create a file in *user’s home/.config/astal directory, but then how will the app use that file? I am getting confuse here, I tried to change the astel.scss file lines to @import "~/.config/astal/scss/themes/astel.scss"; but no cigar. I am missing a piece here, can you please help me?

            Sry I am asking a little too much help now.

            Please consider these sources:

            github.com/mobsenpai/hana

            github.com/mobsenpai/aoi (just changed the name)

            https://aylur.github.io/astal/guide/getting-started/nix

            • balsoft@lemmy.ml
              link
              fedilink
              English
              arrow-up
              2
              ·
              1 day ago

              Your understanding is correct, you have to read that file (~/.config/astal/scss/thmees/astel.css) from your app somehow. I’m just guessing that @import might be compile-time, in which case you’ll have to do it some other way, by reading this file at runtime and using it to configure the colors. If @import is runtime itself, perhaps you need to pass the entire path instead of using ~, like @import "/home/mobsenpai/.config/astal/scss/themes/astel.scss" or something.

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

                I don’t think there is a way to give the config during runtime or while calling the binary of the app, I will keep trying something, one thing I think that may help is creating that file during the build process, but I just don’t have enough knowledge to do that.

                • balsoft@lemmy.ml
                  link
                  fedilink
                  English
                  arrow-up
                  2
                  ·
                  1 day ago

                  one thing I think that may help is creating that file during the build process

                  This is certainly an option, but I would only use it as a last resort as it means recompiling the project every time you want to change the theme. It’s quite easily done though, you can use overrideAttrs on the package in your home-manager module to pass in the desired colors.