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

Make escapeSystemdPath implement the correct systemd escaping algorithm #177273

Merged
merged 5 commits into from
Oct 20, 2022

Conversation

dali99
Copy link
Member

@dali99 dali99 commented Jun 11, 2022

Description of changes

There's a regression in regards to systemd mount units on 22.05:
Since #152372 we validate unit names but that validation isn't the same as what escapeSystemdPath does, notably it doesn't replace ( ) in my case
filesSystems uses escapeSystemdPath to convert the mountpoint to the unit name.

I've added a few functions to help convert the paths to the proper C escapes implementing the algorithm described in man systemd.unit But this is my first time working or changing anything significant in lib, so I'd be really happy for pointers on how to do this better utilizing functions we maybe already have?

I tested this on my configuration (cherry-picked to 22.05) and it seems fine... but this is a basically-8 year old function I'm changing which is pretty scary...

I also only did ASCII special chars, but without something like a builtins function for the UTF-8 conversion stuff I don't know if it's possible to generalize

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.11 Release Notes (or backporting 22.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added the 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS label Jun 11, 2022
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Jun 11, 2022
@ajs124
Copy link
Member

ajs124 commented Jun 12, 2022

Just commenting so this isn't only documented on matrix. I also tried to fix this recently, in #176115.

So far, we've figured out that this PR here properly translates characters, while mine handles the / special case and deduplicates consecutive /s.

I don't really have any preference over which PR should be merged into which one or how else to proceed, as long as we have an implementation that's as complete as possible, in the end.

@dali99 dali99 force-pushed the escape-systemd branch 3 times, most recently from 71dbed9 to dc8223a Compare June 12, 2022 12:43
@dali99 dali99 requested a review from ajs124 June 12, 2022 12:50
@dali99 dali99 changed the title escape systemd paths maybe Make escapeSystemdPath implement the correct systemd escaping algorithm Jun 12, 2022
@dali99 dali99 marked this pull request as ready for review June 12, 2022 13:31
@Mindavi
Copy link
Contributor

Mindavi commented Jun 19, 2022

Can we add tests to make the behavior more clear and to document use cases? Also can help fixing any issues we find after getting this in, by improving the tests then.

@dali99
Copy link
Member Author

dali99 commented Jun 23, 2022

@Mindavi We don't seem to have any testing infrastructure for nixos/lib So I've only added tests for the new string functions (and only positive ones at that)

I feel like the escapeSystemdPath is complicated enough to warrant some testing but I'm not sure if this PR is the right place to add tests to nixos/lib

@tadfisher
Copy link
Contributor

An issue should be opened in nix to provide a built-in for string formatting. So much of this would be a simple printf in C, even codepoint conversion.

lib/strings.nix Outdated
@@ -305,6 +330,18 @@ rec {
*/
escape = list: replaceChars list (map (c: "\\${c}") list);

/* Escape occurence of the element of `list` in `string` by
converting to it's ASCII value and prefixing it with \\x.
Copy link
Member

Choose a reason for hiding this comment

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

mention that it only works with printable ascii characters. For non printable ones, I wonder if it is correct to map the caracter 7 to \x7 instead of \x07.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've now documented that it only works for printable characters, You're quite right about the non-printable ones as well, but I think we would need a nix builtin to deal with those anyways

nixos/lib/utils.nix Outdated Show resolved Hide resolved
@dali99 dali99 force-pushed the escape-systemd branch 2 times, most recently from eab9341 to 83ac459 Compare August 19, 2022 01:34
@dali99 dali99 requested a review from symphorien September 3, 2022 15:05
@symphorien
Copy link
Member

@GrahamcOfBorg test systemd

@symphorien
Copy link
Member

(note that github does not always send notifications on force-push, so when you address a review, please post a full-fledged comment)

Copy link
Member

@symphorien symphorien left a comment

Choose a reason for hiding this comment

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

this breaks nixosTest.systemd (it looks "currently building" but the test vm is just stuck failing to boot)
problem is: the vm uses the autoformat feature, but the unit running mkfs does not run, I don't know why.

@dali99 dali99 requested a review from symphorien October 20, 2022 18:10
@dali99
Copy link
Member Author

dali99 commented Oct 20, 2022

@GrahamcOfBorg test systemd

@symphorien symphorien merged commit 91a3819 into NixOS:master Oct 20, 2022
@symphorien
Copy link
Member

thank you!

@zowoq
Copy link
Contributor

zowoq commented Oct 22, 2022

nix-build -A nixosTests.simple --max-jobs 0 --eval-store auto --store ssh-ng://**** --system x86_64-linux
error: invalid regular expression ''

       at /Users/zowoq/src/nixpkgs/lib/strings.nix:540:51:

          539|       s = builtins.unsafeDiscardStringContext _s;
          540|       splits = builtins.filter builtins.isString (builtins.split (escapeRegex sep) s);
             |                                                   ^
          541|     in
(use '--show-trace' to show detailed location information)

Only seems to be a problem when running nixosTests remotely? Works again after reverting 3251123.

@symphorien
Copy link
Member

symphorien commented Oct 22, 2022 via email

@zowoq
Copy link
Contributor

zowoq commented Oct 22, 2022

Yeah, only seems to be darwin->linux. If it is a nix bug I doubt it will be quick or simple to fix. I'd like to revert this PR until it is resolved.

@dali99
Copy link
Member Author

dali99 commented Oct 22, 2022

I'd be okay with that if we also reverted #152372 I don't want to maintain a nixpkgs channel with this patch for this coming release as well

@symphorien
Copy link
Member

So the code in this PR does not involve regex at all except with the hard coded regex "/" and "" which are valid, could you report the error with a stacktrace and possibly with --debugger so that we know the value of the faulty regex?

@symphorien
Copy link
Member

(besides this is a bugfix in nixos code that is only faulty when evaluating code on mac, which is more niche than the use cases it fixes imo, so I'd rather fix this than revert it).

@zowoq
Copy link
Contributor

zowoq commented Oct 23, 2022

Can you suggest another workaround apart from reverting this locally on every branch that I'm working on?

I don't see that it matters if we revert it for a bit so we aren't introducing new breakage.

@dali99
Copy link
Member Author

dali99 commented Oct 23, 2022

This fixes a (at the time new) breakage on actual NixOS systems which IMO probably takes priority over evaluating nixos tests from darwin. I believe one can just ssh to a Linux box and run tests there in the mean time.

This exposing something broken in nix on darwin hopefully incentives someone who has access and cares about darwin to investigate and follow up the bug.

@zowoq
Copy link
Contributor

zowoq commented Oct 23, 2022

This fixes a (at the time new) breakage on actual NixOS systems which IMO probably takes priority over evaluating nixos tests from darwin.

It is old code that has been broken for months, there certainly didn't seem to be any urgency in merging this PR. I don't see the problem with reverting it for a bit.

I believe one can just ssh to a Linux box and run tests there in the mean time.

Doesn't seem to be that simple as would need to sync code as well?

This exposing something broken in nix on darwin hopefully incentives someone who has access and cares about darwin to investigate and follow up the bug.

Yes, I'll try to do that. Not having my current workflows broken would mean I have more time to try to fix it?

symphorien added a commit to symphorien/nixpkgs that referenced this pull request Oct 23, 2022
presumably due to using libc++'s regex lib instead of libstdc++ on linux

Fixes NixOS#177273 (comment)
@symphorien
Copy link
Member

@zowoq: can you try #197374 ?

@symphorien
Copy link
Member

The discrepancy is problematic so I opened NixOS/nix#7208

zowoq pushed a commit that referenced this pull request Oct 24, 2022
presumably due to using libc++'s regex lib instead of libstdc++ on linux

Fixes #177273 (comment)
@zowoq
Copy link
Contributor

zowoq commented Oct 24, 2022

Seems this is a recurring problem.

NixOS/nix#1537 (comment)
Empty regular expressions are not allowed according to the POSIX extended regex grammar

github-actions bot pushed a commit to arcnmx/nixpkgs-lib that referenced this pull request Oct 25, 2022
presumably due to using libc++'s regex lib instead of libstdc++ on linux

Fixes NixOS/nixpkgs#177273 (comment)
github-actions bot pushed a commit to nix-community/nixpkgs.lib that referenced this pull request Oct 30, 2022
presumably due to using libc++'s regex lib instead of libstdc++ on linux

Fixes NixOS/nixpkgs#177273 (comment)
github-actions bot pushed a commit to nix-community/nixpkgs.lib that referenced this pull request Dec 4, 2022
presumably due to using libc++'s regex lib instead of libstdc++ on linux

Fixes NixOS/nixpkgs#177273 (comment)
github-actions bot pushed a commit to nix-community/nixpkgs.lib that referenced this pull request May 31, 2023
presumably due to using libc++'s regex lib instead of libstdc++ on linux

Fixes NixOS/nixpkgs#177273 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants