Skip to content

nixos/etc: Rework make-etc.sh#131035

Closed
dasJ wants to merge 1 commit intoNixOS:masterfrom
helsinki-systems:feat/rework-etc
Closed

nixos/etc: Rework make-etc.sh#131035
dasJ wants to merge 1 commit intoNixOS:masterfrom
helsinki-systems:feat/rework-etc

Conversation

@dasJ
Copy link
Member

@dasJ dasJ commented Jul 22, 2021

This commit changes the data format from 5 arrays to one big JSON file
which ensures special characters (like \n) don't break anything.

For this to work, make-etc.sh needs jq and will create a big list from
\0-separated values (see the comment in the file).

Also this commit quotes all variables, sets useful shell options,
and fixes all shellcheck issues. Additionally, we show all errors before
exiting instead of exiting on the first error. And finally, rename
source to src since source is a bash keyword.

Closes #130935
Closes #131102
Closes #108779
Closes #41241
Closes #41276

Motivation for this change
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) → simple.nix
  • 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/)
  • 21.11 Release Notes (or backporting 21.05 Relase 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
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Jul 22, 2021
@dasJ dasJ requested review from ctheune, danbst, infinisil and zowoq July 22, 2021 07:54
@dasJ
Copy link
Member Author

dasJ commented Jul 22, 2021

The \n problem seems to be resolved by this:

image

@dasJ dasJ force-pushed the feat/rework-etc branch 2 times, most recently from 9c1e6d4 to afc983c Compare July 22, 2021 07:59
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Jul 22, 2021
Comment on lines +18 to +22
Copy link
Member

Choose a reason for hiding this comment

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

This still seems a little hacky to me. How about doing this instead, I used this myself before too when ingesting json output:

jq '.[]' -c "$entriesPath" | while read -r line; do
  src=$(jq -r '.source' <<< "$line")
  target=$(jq -r '.target' <<< "$line")
  # ...

The -c makes sure that each list entry is output on a separate line as JSON

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 had that idea as well but it's horribly slow in practice. It took like 10 seconds for my local system

Copy link
Member

Choose a reason for hiding this comment

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

Oh damn, yeah I guess it's a whole bunch of jq calls..

Possibly @sh could be used in some way to make this a bit nicer

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 tried that. @ajs124 helped me like 2 hours to get it to work using @sh but we were unable to. The main issue is that you get \n-separated values which you put into something like while read -r source target …; do. This however breaks when your value contains \n since it's a literal newline and that results in the loop running twice for half an entry each

Copy link
Member

@ajs124 ajs124 Jul 22, 2021

Choose a reason for hiding this comment

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

Which could potentially be fixed with \0 separated output, but jq doesn't support that in any released version.

Edit: Nvm, you're already doing that with that hack.

Copy link
Member

Choose a reason for hiding this comment

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

Alternative idea: What if we sidestep JSON and jq completely by just generating the shell script in Nix itself. So instead of a shell for loop, we have a lib.concatMapStringsSep or so. This will generate a fairly big script, but I don't see a problem with that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Won't this cause the same problems as svanderburg/node2nix#46 (comment)?

Copy link
Member

Choose a reason for hiding this comment

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

I'd think the scales we are dealing with are a bunch smaller.

Copy link
Contributor

Choose a reason for hiding this comment

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

On my systems the etc derivation is typically around 20k with 150-200 entries for the sources. So yes, that's a lot smaller.

This commit changes the data format from 5 arrays to one big JSON file
which ensures special characters (like \n) don't break anything.

For this to work, make-etc.sh needs jq and will create a big list from
\0-separated values (see the comment in the file).

Also this commit quotes all variables, sets useful shell options,
and fixes all shellcheck issues. Additionally, we show all errors before
exiting instead of exiting on the first error. And finally, rename
source to src since source is a bash keyword.

Closes NixOS#130935
@dasJ
Copy link
Member Author

dasJ commented Jul 22, 2021

Alternative implementation: #131102

@dasJ dasJ closed this Jul 30, 2021
@dasJ dasJ deleted the feat/rework-etc branch July 30, 2021 19:33
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 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong characters in etc."xyz".source causes system crash / unbootable system Whitespace in environment.etc target crashes nixos

4 participants