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

nixos/nixpkgs/lib/strings: add stringToValidNixPath #83223

Closed
wants to merge 3 commits into from
Closed

nixos/nixpkgs/lib/strings: add stringToValidNixPath #83223

wants to merge 3 commits into from

Conversation

rpearce
Copy link
Contributor

@rpearce rpearce commented Mar 23, 2020

(This is my first time contributing like this to nixpkgs... please be gentle)

Motivation for this change

When using tools like nixpkgs.lib.attrsets.mapAttrsToList
and trying to build derivations where nix store file names
are to be created, set attributes like ".stack/config.yml"
are invalid, so sanitizeNixPath cleans them up to allow
them to be created in the nix store.

I did not include - as a valid character because of rycee's reasoning here.

If no prefix is provided, the string safePrefix_ is used.

Things done
  • adds strings.sanitizeNixPath
  • adds tests for strings.sanitizeNixPath
  • 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.

I'm not sure how to run the tests locally and could use some help doing that.

When using tools like nixpkgs.lib.attrsets.mapAttrsToList
and trying to build derivations where nix store file names
are to be created, set attributes like ".stack/config.yml"
are invalid, so stringToValidNixPath cleans them up to allow
them to be created in the nix store.
lib/strings.nix Outdated Show resolved Hide resolved
@Mic92 Mic92 requested a review from Profpatsch March 23, 2020 16:37
@rpearce rpearce marked this pull request as ready for review March 23, 2020 16:43
@infinisil
Copy link
Member

I also have an implementation of this in #78640, see 30ad4ee

I could put that into its own PR

@rpearce
Copy link
Contributor Author

rpearce commented Mar 23, 2020

@infinisil If that's better than this, then by all means go for it. Or if you think I could adjust this to incorporate some of your concerns, like length, then I'm happy to update this (with your guidance) and accomplish the same goal


sanitizeNixPath = prefix: path:
let
safePrefix = if replaceStrings [" "] [""] prefix == "" then "safePrefix_" else prefix;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should prefixing be left to consumers to figure out themselves?

@infinisil
Copy link
Member

I'm obviously biased towards my solution :). Some arguments for my solution are that it uses regex-based replacement, which should be faster than builtins.replaceStrings and the code is very clear as to what it does.

@rpearce
Copy link
Contributor Author

rpearce commented Mar 23, 2020

@infinisil If you get that into a PR, I'll test it out for you with what I'm testing with on my side, and if we get that to 👍, I'll close this. How does that sound?

@infinisil
Copy link
Member

Done that :) #83241

@rpearce rpearce closed this Mar 23, 2020
@rpearce rpearce deleted the strings/stringToValidNixPath branch March 23, 2020 20:17
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

4 participants