fomm
May 29, 2023, 3:53pm
1
Hi,
This is my first attempt to modify a home manager module and I could not find a tutorial around how to do it.
What I want to do is to override the home manager espanso service module
The reason is that it does not support configurations.
As we can see from official doc
The folder structure should look like below.
config/
default.yml
match/
base.yml
Because of this problem, when I enable espanso service, I am getting below error.
I am thinking to download originl file, add configs
under settings and output two yamls into two different folder. To verify that I need a way to override the home manager module to use my local one. Can anyone please advise how I can override this module?
Thank you
1 Like
I do not have this issue but maybe this:
{ config, ...}:
{
config.xdg.configFile = {
"espanso/config/default.yml".source = xdg.configFile."espanso/default.yml".source;
"espanso/match/base.yml".source = config.services.espanso.settings.matches
};
}
edit: now I know what match expects
fomm
May 31, 2023, 1:37am
3
I am making some progress by putting below on home.nix
file.
disabledModules = ["services/espanso.nix"];
Then I can import my local espanso.nix
to home manager as
homeConfigurations = {
<user> = home-manager.lib.homeManagerConfiguration {
inherit pkgs;
modules = [
./home.nix
./modules/espanso.nix
{
nixpkgs.overlays = [
nur.overlay
];
}
];
};
};
Now I need to figure out how to write options to create dynamic files under espanso/config
folder.
For example nix should generate vscode.yml
and vscodium.yml
dynamically and put them under the config folder as below.
config/
vscode.yml
vscodium.yml
It would be really great to have an updated espanso module in home-manager as well. Are you planning to upstream your module?
For the separate espanso files, you can use functions like mapAttrsā to convert configs to file attrsets. For instance:
config.xdg.configFile =
let
espansoConfigs = {
default = { ... };
vscode = { ... };
};
in
lib.mapAttrs' (name: value: {
name = "espanso/config/${name}.yml";
value = {
source = yaml.generate "${name}.yaml" value;
};
}) espansoConfigs;
fomm
June 1, 2023, 2:45pm
5
Thanks for the reply.
If I could figure out a solution which is compatible with existing user, I might upstream the moduleā¦
Unfortunately, given my limited Nix knowledge, this might take a while.
What I am thinking now is very simple. To make this compatible for existing user, we might just add configs under settings like below.
options = {
services.espanso = {
enable = mkEnableOption "Espanso: cross platform text expander in Rust";
package = mkOption {
type = types.package;
description = "Which espanso package to use";
default = pkgs.espanso;
defaultText = literalExpression "pkgs.espanso";
};
settings = mkOption {
type = yaml.type;
default = {
configs = [];
matches = [];
};
};
};
};
When user have configs like below on for their home.espanso.settings
, the module should generate 4 yaml files under ~/.config/espanso/config
folder.
configs = [
default = {};
vscode = {};
vscodium = {};
abc = {};
]
Not too sure how to write it though.
It could look something like:
config.xdg.configFile = lib.mapAttrs' (name: value: {
name = "espanso/config/${name}.yml";
value = {
source = yaml.generate "${name}.yaml" value;
};
}) config.services.espanso.settings.configs;
Iād personally make settings split into 2 options: configs
and matches
. Otherwise you can do something like:
services.espanso.settings.somethingelse = 3;
And have no idea what it should be doing
configs = mkOption {
type = types.attrsOf yaml.type;
default = {};
};
Iām not entirely sure whether matches needs to be in separate files. I think itās fine to put all matches in a single file:
matches = mkOption {
type = yaml.type;
default = {};
};
Combining matches and config files can be done like:
config.xdg.configFile =
let
configFiles = lib.mapAttrs' (name: value: {
name = "espanso/config/${name}.yml";
value = {
source = yaml.generate "${name}.yaml" value;
};
}) config.services.espanso.configs;
matchesFiles = {
"espanso/matches/base.yml".source = yaml.generate "base.yaml" config.services.espanso.matches;
};
in configFiles // matchesFiles;
1 Like
fomm
June 5, 2023, 4:04pm
7
Thanks for the detail, this certainly generates the file at the desire location.
I thought about splitting matches and configs before but this will be a breaking change. Maybe I should use something like mkIf (cfg.settings != { })
to make the new module compatible with current user.
Now the final issues I run into.
I canāt find the right combination to generate double quote.
For example, below config
matches = [
{
trigger = ":hello";
replace = ''world'';
}
];
is generated to below on match/base.yaml
which does not workā¦
- replace: world
trigger: :hello
On the example, I need to add double quotes around world
and :hello
.
It becomes more complicated if I need multi-line support like below.
- trigger: "include newlines"
replace: |
exactly as you see
will appear these three
lines of poetry
I know I can use \n
to do it, but it also requires double quotes around the string.
I searched a lot but I could not find a good example on how to manipulate this yaml.generate process.
The problem isnāt quotation. Quotation is not required for these cases, Nix yaml generation only outputs those if theyāre actually needed for yaml. Same for multiline strings. Basically: donāt worry about the yaml formatting.
The problem I think is that the yaml files doesnāt include matches:
.
In your example you can add it using:
matches.matches = [
{
trigger = ":hello";
replace = ''world'';
}
];
It looks a bit weird, but the matches/base.yaml may include more sections. See Organizing matches | Espanso
The current espanso module is already incompatible with espanso 2.x. It would be already fine to add an assertion/rename notice in the module about services.espanso.settings. That way the configuration of users will fail with an descriptive error about settings being deprecated and configs/matches should be used.
It could be backwards compatible, but Iām not sure how maintainable it is going forward in the future.
1 Like
fomm
June 6, 2023, 2:54pm
9
Thank you so much for all the guidance. Now I have a working module.
I agree I might be overthinking the backward compatibility issue as the current config is already broken.
I will do a bit more testing to cover more use cases first and then upstream the module when I am satisfied.
1 Like
Awesome could you link the PR here if/when you have one?
fomm
June 7, 2023, 3:34pm
11
Here is the PR.
nix-community:master
ā liyangau:master
opened 03:22PM - 07 Jun 23 UTC
### Description
Espanso module does not work with 2.x version. In the new ve⦠rsion configuration and matches are put into different folders as below.
```
config/
default.yml
match/
base.yml
```
The updated module supports creating multiple files for both config and matches. Users have the full flexibility to create multiple files for different apps.
### Checklist
- [ ] Change is backwards compatible.
- [x] Code formatted with `./format`.
- [x] Code tested through `nix-shell --pure tests -A run.all` or `nix develop --ignore-environment .#all` using Flakes.
- [x] Test cases updated/added. See [example](https://github.com/nix-community/home-manager/commit/f3fbb50b68df20da47f9b0def5607857fcc0d021#diff-b61a6d542f9036550ba9c401c80f00ef).
- [x] Commit messages are formatted like
```
{component}: {description}
{long description}
```
See [CONTRIBUTING](https://github.com/nix-community/home-manager/blob/master/docs/contributing.adoc#sec-commit-style) for more information and [recent commit messages](https://github.com/nix-community/home-manager/commits/master) for examples.
- If this PR adds a new module
- [ ] Added myself as module maintainer. See [example](https://github.com/nix-community/home-manager/blob/068ff76a10e95820f886ac46957edcff4e44621d/modules/programs/lesspipe.nix#L6).
I would like to give credit to you. Can I add your id as maintainer on the module or co-auther on the PR? (I am not familiar with this co-author, maintainer thing so I figured I should ask first)
Thank you.
1 Like
Sure no problem. I made a small suggestion, but in general it looks like a good or! Thanks for submitting!