Skip to content

Instantly share code, notes, and snippets.

@danbst
Last active November 8, 2017 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbst/a9fc068ff26e31d88de9709965daa2bd to your computer and use it in GitHub Desktop.
Save danbst/a9fc068ff26e31d88de9709965daa2bd to your computer and use it in GitHub Desktop.
Is there simpler way to extract string from Nix string?
function nixeval { eval echo -en "$(nix-instantiate --eval --strict "$@" | sed 's/${/\\${/g')"; }

Evaluate Nix expression and remove quotes from resulting expresssion:

$ nixeval -E 'builtins.toJSON __nixPath' | jq
[             
  {
    "path": "/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs",
    "prefix": "nixpkgs"
  },
  {
    "path": "/etc/nixos/configuration.nix",
    "prefix": "nixos-config"
  },
  {
    "path": "/nix/var/nix/profiles/per-user/root/channels",
    "prefix": ""
  },
  {
    "path": "/nix/store/jckpxyik29cvxyxafhlmi65w8xr3szkd-nix-1.11.11/share/nix/corepkgs",
    "prefix": "nix"
  }
]
@danbst
Copy link
Author

danbst commented Nov 8, 2017

Thanks, @edolstra

$ nix eval -f $(nix-build -E 'with import <nixpkgs>{}; writeText "example.nix" "builtins.toJSON __nixPath"') --raw | jq
[
  {
    "path": "/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs",
    "prefix": "nixpkgs"
  },
  {
    "path": "/etc/nixos/configuration.nix",
    "prefix": "nixos-config"
  },
  {
    "path": "/nix/var/nix/profiles/per-user/root/channels",
    "prefix": ""
  },
  {
    "path": "/nix/store/jbxvhbwzafs4w4di45kwic7rhf3mwdwk-nix-1.12pre5511_c94f3d55/share/nix/corepkgs",
    "prefix": "nix"
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment