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

Consider running a flake in a subdirectory #1164

Open
nick-kadutskyi opened this issue Apr 22, 2024 · 2 comments
Open

Consider running a flake in a subdirectory #1164

nick-kadutskyi opened this issue Apr 22, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@nick-kadutskyi
Copy link

nick-kadutskyi commented Apr 22, 2024

In process of setting up devenv via flake.nix I noticed that my nix store gained 50 gb in size. Turned out Nix is copying the whole source tree into the store for evaluation (I've just started using Nix and didn't know that) and my project is 2 GB so getting it copied on every change in flake.nix makes it very slow and bloats the nix store. In my case I do not need my repo in the nix store because I just need some packages (php) and services (mysql, php-fpm, httpd) and do not want to build the project.

To avoid this I moved flake.nix into a subdirectory (nix) and refer to that flake via path: in .envrc config (use flake path:nix --impure instead of use flake --impure) or run nix develop path:nix --impure. Referring to a flake via path: ignores whatever is tracked in git repo and instead copies all the file tree where flake.nix is located. So having flake.nix in an empty subdirectory will result into copying only nix/flake.nix and nix/flake.lock into the store.

This solves the problem of running nix develop or using direnv but it doesn't solve devenv up because I can't run it from root of the project and in that script flake.nix is referred via git so that if I cd into my subdirectory (nix) and run devenv up it will copy the whole project into the nix store.

To solve this I just created a similar script in my repo and added it into the flake as a module to have it accessible from the terminal as devenv-custom.
The change I made to the script:

...
 command=$1
  if [[ ! -z $command ]]; then
    shift
  fi

+ flakePath=$1
+ if [[ $flakePath == "path:"* ]]; then
+   shift
+ else
+   flakePath="."
+ fi

  case $command in
    up)
-     procfilescript=$(nix build '.#${shellPrefix (config._module.args.name or "default")}devenv-up' --no-link --print-out-paths --impure)
+     procfilescript=$(nix build $flakePath#'${shellPrefix (config._module.args.name or "default")}devenv-up' --no-link --print-out-paths --impure)
      if [ "$(cat $procfilescript|tail -n +2)" = "" ]; then
        echo "No 'processes' option defined: https://devenv.sh/processes/"
        exit 1
      else
...

This allows me to

  • run devenv-custom up path:nix from project root
  • refer to my flake.nix via path:nix to avoid copying the whole repo into the nix store
  • still use it as originally intended

I am ok running my own script but I propose to consider this change at least until this PR is merged.

@nick-kadutskyi nick-kadutskyi added the enhancement New feature or request label Apr 22, 2024
@domenkozar
Copy link
Member

If you use devenv binary you won't have this issue.

@nick-kadutskyi
Copy link
Author

nick-kadutskyi commented Apr 23, 2024

@domenkozar when I run nix develop path:nix --impure I am entering shell where I get a different version of devenv (bash script) which has only up and version commands.
Even if I use the devenv binary (installed in my nix profile) I think I will get into the same problems:

  • I won't be able to do devenv up from root of my project when flake.nix is located in ./nix dir.
  • If I cd into my subdirectory and run up cmd it will copy all the files tracked by git into the store because rust binary calls nix build command in a similar way as bash scripted devenv. So it still refers to flake via .#procfileScript meaning it refers to it via git+file: and that will copy all the files tracked by my local git repo.

Both devenv script and binary will work in my case if I would not have a git repo. In a non-git project nix build .#procfileScript will be treated as nix build path:.#procfileScript wile in git repo it's treated as nix build git+file:.#procfileScript (See Path-like syntax) so if I want to still refer to it as path: while in git repo I need to do that explicitly.

Also devenv binary requires devenv.nix instead of flake.nix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants