How can I install a package using overlays and flakes?

Hey all.
I’m trying to figure out how overlays work in flakes. I would appreciate it if you can tell me what steps to take and why.
For example, there is a package called espanso.
Currently, the version in nixos unstable repository is 0.7.3 and I’m trying to install the tag version 2.1.0-alpha from github.

I would prefer to use the services.espanso.enable in my configuration.nix for the sake of simplicity, however there is no services.espanso.package option available. (though I’m not insisting on this option if it cause more complexity than otherwise)
My dotfiles structure is as follows:

dotfiles
├── system                  # system level configs
│   ├── configuration.nix   
│   └── ...  
├── user                    # home-manager and other dotfiles
│   ├── home.nix            # 
│   ├── alacritty
│   └── ...      
├── flake.nix
└── ...   

Other information: espanso is at "github:federico-terzi/espanso" and it offers no flakes. It is also written in rust. Please consider that these are for the sake of example, and I really hope to learn the simplest workflow (and hopefully a bit about how flake files and overlays work).
I have read a lot of dotfiles but couldn’t understand why they did what they did, what was necessary and how I can implement similar things.
My endless appreciations, in advance :slight_smile:

2 Likes

The easiest way to use overlays with a flake is to use something like flake-utils-plus to manage your flake. To add an overlay a channel, just specify it in your channels.<channel>.overlayBuilder function. You can see an example of this in my configs repo.

If you want to do things manually or don’t want to make the changes needed to move to a flake library, you can include an attribute set in the modules that you pass to the nixosSystem. For example:

# Assume that `system` specifies the current system (e.g., x86_64-linux),
# and that `espansoRepo` is an input with the `espanso` package
# in a `packages` attribute.
nixosConfigurations.<hostname> = nixpkgs.lib.nixosSystem {
  modules = [
    {
      nixpkgs.overlays = [
        (_: _: { espanso = espansoRepo.packages.${system}.espanso; })
      ];
    }
    # other modules, etc
  ];
};
3 Likes

Thanks for your suggestion.
I’m trying nix-utils-plus. I’m however not sure exactly how I should achieve it. I made a flake based on the examples on the github page + your dotfiles. And I got this error:

error: source tree referenced by 'github:federico-terzi/espanso/f7fdb06db89fe6f33c92c9bb2568cbfa40761028' does not contain a '/flake.nix' file

Here is my flake.nix:

{
  description = "dotfiles";
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";
    utils.url = "github:gytis-ivaskevicius/flake-utils-plus/v1.3.0";
    nur.url = "github:nix-community/NUR";
    home-manager = {
      url = "github:nix-community/home-manager/master";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    emacs-overlay = {
      type = "github";
      owner = "nix-community";
      repo = "emacs-overlay";
    };
  };
  outputs = inputs@{ self, utils, nixpkgs, home-manager, emacs-overlay, ... }:
    let
      inherit (nixpkgs.lib) recursiveUpdate;
      # overlays = import ./overlays;
      # packages = import ./pkgs;
    in utils.lib.mkFlake rec {
      inherit self inputs;
      channels.nixpkgs = {
        config.allowUnfreePredicate = pkg:
          builtins.elem (nixpkgs.lib.getName pkg) [ ];
        overlaysBuilder = channels:
          [
            # overlays
            (_: _: { espanso = channels.nixpkgs.espanso; })
          ];
      };
      hostDefaults.modules =
        [ home-manager.nixosModules.home-manager ./system/configuration.nix ];
    };

}

What am I missing?
Thanks again!

1 Like

Is the repo mentioned in the error yours? Or where does it come from?

Well, no, it is not mine. It is the developer’s repo. Should I fork and add my own flake to it?

I’d probably just copy the derivation, update it to build the build the 2.1.0-alpha, and include that in the overlay.

I put together a gist of what that might look like. It’s still building, so I don’t know if it works (though I don’t have a desktop Linux system to test anyway).

overlaysBuilder = channels:
  [
    # overlays
    (_: _: { espanso = channels.nixpkgs.callPackage ./espanso.nix {}; })
  ];
2 Likes

Oh my god! That is so kind of you! I’m trying it right now! I think I can understand what happened now and what I should do! Thank you very much!

2 Likes

You’re welcome!

Alas, the build failed again. It looks like Espanso also depends on wxWidgets. I updated the gist to add wxGTK31. Hopefully that’s the last of them. I think the other ones mentioned on the project’s GitHub page are already included.

Shouldn’t this thread be moved to Learn category?

1 Like
Hosted by Flying Circus.