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

build-support: add strings-replace tool #22323

Closed
wants to merge 1 commit into from

Conversation

shajra
Copy link
Contributor

@shajra shajra commented Jan 31, 2017

This commit introduces a tool called "strings-replace" that allow for a simple substitution of strings as as discovered by

strings -t d "$target" | grep e "$pattern"

Because this is a substitution of a null-terminated string into a binary, there's a fail-safe to make sure the substituted string is not longer than the original string.

This is my first commit to nixpkgs, and I read through all the contributors documentation. Hopefully I didn't miss anything, but I'm happy to make changes as recommended.

Motivation for this change

This is similar to nuke-refs, only not /nix/store specific. When attempting to make minimal closures, sometimes various build tools end up with hardcoded references to items in /nix/store. nuke-refs is nice if these references are dead references. But sometimes, you want to move these files to another derivation and swap these new locations into the binary file.

An example of this problem is Haskell Cabal, which has a feature called "data-files". For example, a popular Haskell monitoring library called ekg uses this feature to compile in references to web assets like HTML and Javascript files.

We can have Cabal/GHC statically compile binaries to reduce the size of the transitive closure dramatically. But this doesn't help with strings compiled into the data-section of a binary.

This problem has been discussed in Gabriella439/haskell-nix#12 and #4504.

Even if Cabal/GHC were fixed such that this weren't needed for ekg, I think this might be a useful tool to have available. It's a sharp knife, though, so care must be exercised when using it.

Things done
  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • Linux
  • Testedcompilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

This is similar to `nuke-refs`, only not /nix/store specific.  When
attempting to make minimal closures, sometimes various build tools end
up with hardcoded references to items in /nix/store.  `nuke-refs` is
nice if these references are dead references.  But sometimes, you want
to move these files to another derivation and swap these new locations
into the binary file.

An example of this problem is Haskell Cabal, which has a feature called
"data-files".  For example, a popular Haskell monitoring library called
`ekg` uses this feature to compile in references to web assets like HTML
and Javascript files.

We can have Cabal/GHC statically compile binaries to reduce the size of
the transitive closure dramatically.  But this doesn't help with strings
compiled into the data-section of a binary.

So this commit introduces a tool called "strings-replace" that allow for
a simple substitution of strings as as discovered by

    strings -t d "$target" | grep e "$pattern"

Because this is a substitution of a null-terminated string into a
binary, there's a fail-safe to make sure the substituted string is not
longer than the original string.
@edolstra
Copy link
Member

Note that there already is a replace-literal command (in the replace package). IIRC it handles binaries correctly, but I'm not sure. Once upon a time it was part of stdenv (used in the implementation of substitute).

@shajra
Copy link
Contributor Author

shajra commented Jan 31, 2017

@edolstra it's great to know that's there. I have a repository where I'm testing this out. Let me try to use replace instead of my strings-replace. It should be fine. I'll do that soon (today or tomorrow, hopefully). If replace meets my needs, then we can close this request without merging. There's no reason to add unneeded complexity to nixpkgs. Thanks!

@shajra
Copy link
Contributor Author

shajra commented Jan 31, 2017

Hey, I just updated my example-nix project to use replace-literal instead of strings-replace and everything works just fine. I think we can just close this request without merging. I'll do that to save you the hassle.

@shajra shajra closed this Jan 31, 2017
@shajra shajra deleted the feature/strings-replace branch February 9, 2017 15:35
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