Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ankisyncd, nixos/ankisyncd: init at 2.1.0 #82185

Merged
merged 3 commits into from Mar 12, 2020
Merged

Conversation

matt-snider
Copy link
Contributor

Motivation for this change

ankisyncd is a python application for synchronizing Anki data.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@matt-snider
Copy link
Contributor Author

@tsudoko Just a heads up that I've packaged this for Nix

Copy link
Member

@Ekleog Ekleog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you! Feel free to ignore the two nits, which are mostly “this is the style I prefer” changes. However, the question about the name of the executable is important, I think, in order to avoid future breaking changes should it have been a mistake.

Also, I see you created the users with users.users and created the directory with systemd.tmpfiles. This works perfectly well, but nowadays we try to limit our user creation and use DynamicUsers as much as possible, like https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/freeswitch.nix#L86-L101 .
Do you think that this solution would be possible to apply to anki-sync-server?

pkgs/servers/ankisyncd/default.nix Outdated Show resolved Hide resolved
pkgs/servers/ankisyncd/default.nix Outdated Show resolved Hide resolved
pname = "ankisyncd";
version = "2.1.0";
src = fetchFromGitHub {
owner = "tsudoko";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsudoko Have you tried to ask for getting in @ankicommunity, so that it would be possible to have the more legitimate-looking url here?

This concern is not blocking merging though, as internet appears to confirm that the ankicommunity one is dead for the time being and yours is the current one.

nixos/modules/services/misc/ankisyncd.nix Outdated Show resolved Hide resolved
@matt-snider
Copy link
Contributor Author

@Ekleog Thanks for the review - it's great to get feedback so fast! I've taken care of most the issues. I also found a few other minor things, like for example using cfg.package instead of pkgs.ankisyncd

I realize now that I should have pushed the fixes regularly, so it's easier to review, and not squashed and force pushed until the end. Sorry about that.

Regarding DynamicUser, this seems like a good idea. If I add DynamicUser = true, and leave the systemd.tmpfiles.rules and the unit's User and Group settings as they are, it works. Is that what you were thinking, or did you intend to also avoid specifying the User and Group?

Without specifying these it seems that the permissions in the systemd.tmpfiles.rules are not properly set. The error is Changing to the requested working directory failed: Permission denied. Indeed, the documentation says Z lines are ignored for "-".

systemd.tmpfiles.rules = [
  "d '${cfg.dataDir}' 0770 - - - -"
  "Z '${cfg.dataDir}' 0770 - - - -"
];

serviceConfig = {
  Type = "simple";
  DynamicUser = true;
  WorkingDirectory = cfg.dataDir;
  ExecStart = "${cfg.package}/bin/ankisyncd";
  Restart = "always";
};

I'm wondering if DynamicUser is safe in this instance, because the data is persistent. It might be okay if the systemd.tmpfiles.rules specify the user and group. Here's what the DynamicUser= documentation has to say about it:

Care should be taken that any processes running as part of a unit for which dynamic users/groups are enabled do not leave files or directories owned by these users/groups around, as a different unit might get the same UID/GID assigned later on, and thus gain access to these files or directories.

Do you have any advice on this? I don't have that much experience with systemd units.

@Ekleog
Copy link
Member

Ekleog commented Mar 10, 2020

I was thinking of also removing completely User and Group and system.tmpfiles.d settings, and instead use StateDirectory as a way to handle the state. This setup should be supported by systemd, thus avoiding the uid reuse issue :)

As for the force-pushing, do as you feel easiest! Github provides a link on the force-pushed words anyway, that allows seeing the diff from before and after a force push, so it's not a big deal :)

@matt-snider
Copy link
Contributor Author

That works and results in a lot less code - great tip! I wasn't familiar with StateDirectory, but yes it looks like it does take care of the uid reuse issue:

If DynamicUser= is used in conjunction with StateDirectory=, CacheDirectory= and LogsDirectory= is slightly altered: the directories are created below /var/lib/private, /var/cache/private and /var/log/private, respectively, which are host directories made inaccessible to unprivileged users, which ensures that access to these directories cannot be gained through dynamic user ID recycling. Symbolic links are created to hide this difference in behaviour. Both from perspective of the host and from inside the unit, the relevant directories hence always appear directly below /var/lib, /var/cache and /var/log.

Let me know how that looks now

@Ekleog
Copy link
Member

Ekleog commented Mar 12, 2020

It looks great, thank you!

@Ekleog Ekleog merged commit 06bdfc5 into NixOS:master Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants