holochain / holonix-archive

NixOS && Holochain
150 stars 20 forks source link

support config.json analogy to config.nix #86

Closed thedavidmeister closed 1 year ago

thedavidmeister commented 4 years ago

the less nix-fu that beginners need to get started and keep moving, the better

one clear hurdle is the "scary" nix language

once you are familiar with the nix syntax it is easy to forget that it isn't immediately obvious these are all analogous:

nix:

"foo"
[1 2 3]
{ foo = "bar"; }

json:

"foo"
[1, 2, 3]
{ "foo": "bar" }

but, there is a 1:1 mapping that makes sense, so nix natively supports parsing JSON files as nix data, so we should be able to update the default.nix boilerplate to try a holonix.json file if it exists

this would make the boilerplate look gnarlier, but the config more familiar to anyone who has used e.g. package.json style configuration

given that package.json is used to define "scripts" that work with npm run ... in almost the same way (conceptually, i.e. putting bins on the path) as nix-shell --run ... (but limited to the node ecosystem), i'm hoping there's a way to make holonix.json "feel like home" for more people

from @pospi in the forums

If there’s a way that repositories can be setup such that contributors don’t have to think about Nix (including with the addition of custom repo-specific commands), it would be great to have instructions as to how to achieve that. Currently (probably due to legacy) my team is expected to reason about whether they’re in “normal mode” or “nix mode” and that creates room for avoidable errors.

https://forum.holochain.org/t/what-is-the-worst-of-holonix-onboarding-2019-q3/547/3?u=thedavidmeister

pospi commented 4 years ago

There is overlap here but I'm not sure these two things are exactly equivalent. JSON config format would be cool, that's one aspect.

The rest of the request you're quoting from it looks like you're ticking off in #77 but I don't see a complete solution in there. What I'm wondering is: does a command format exist that will not reinitialise the Nix shell if it is already active? Then I can use the same command to run integration tests efficiently whether I am in a Nix environment or not- thus removing the need for developers to "reason about whether they’re in “normal mode” or “nix mode”".

thedavidmeister commented 4 years ago

@pospi this is pretty different, there is already native parseJson type functionality in nix so it would just be "if holonix.json exists, use it"

what type of command do you mean?

pospi commented 4 years ago

does a command format exist that will not reinitialise the Nix shell if it is already active?

So the conditions I want to satisfy are to create some "command" wrapping an "underlying command" such that:

Basically because for newcomers, having those things as 2 separate commands which require you to reason about "am I inside Nix?" in order to know which run to run is a barrier to entry.

thedavidmeister commented 4 years ago

@pospi ok, so some kind of "ensure nix" command/script

you really just need some environment variable that you trust to indicate that you're in nix

printenv | grep NIX

gives a bunch of options (look for something that exists in the shell but not outside)

something like $IN_NIX_SHELL (which can be empty/pure/impure i think) looks perfect as it's also compatible with detection inside an actual nixos machine

echo 'if [[ -z $IN_NIX_SHELL ]]; then nix-shell; fi;' >> ensure-nix.sh
chmod +x ensure-nix.sh
./ensure-nix.sh
./ensure-nix.sh

from here you can guard any further commands you want to run with ./ensure-nix.sh at the start

pospi commented 4 years ago

I figured there might be something like that and was thinking about looping back to say so, so thanks for jumping ahead (:

I don't think it's quite this simple though, and I lack the bash-fu to know how to deal with subshells. But what will happen with the solution above AFAIK is that nix-shell will halt the ensure-nix.sh script and execution will only be picked back up when it exits.

The thing I can't figure out in the approach I'm trying is just bash syntax- I need to unpack all args into a single-quoted string in order to pass to nix-shell --run AFAIK?

if [[ -z $IN_NIX_SHELL ]]; then
  nix-shell --run "$@"
else
  eval "$@"
fi

The goal is that you could basically just prepend this script name to any command you might need to run and have it execute as you would expect. I've been testing with ensure-nix.sh which npm, for example.

thedavidmeister commented 4 years ago

@pospi oh yes, i thought that's what you wanted, to be dropped into a shell if needed

yes if you want to make it "one shot" then do something like --run

pospi commented 4 years ago

I still don't really know how to do it... looks like passing multiple shell args as a single arg isn't a thing that can really be done robustly? Surely there is another way to get nix-shell to accept a multi-word command to run?