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

Add fromFlakeRoot option, start processes in $FLAKE_ROOT by default. #29

Closed
wants to merge 2 commits into from

Conversation

MrFoxPro
Copy link

No description provided.

@MrFoxPro MrFoxPro changed the title feat: add fromFlakeRoot option, run start processes in $FLAKE_ROOT Add fromFlakeRoot option, start processes in $FLAKE_ROOT by default. Jun 30, 2023
@MrFoxPro MrFoxPro changed the title Add fromFlakeRoot option, start processes in $FLAKE_ROOT by default. Add fromFlakeRoot option, start processes in $FLAKE_ROOT by default. Jun 30, 2023
@MrFoxPro
Copy link
Author

Maybe warn if $FLAKE_ROOT wasn't found? Not sure how to implement that warn message

Copy link
Member

@srid srid left a comment

Choose a reason for hiding this comment

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

Is this for #25 (which I didn't entirely understand)?

The problem with this PR is that it will work in some cases (eg: devShell), but not in other cases (eg: nix run). Unless you disable auto wiring (#27), don't add packages.* and say "My process-compose configuration is meant to work only in the devShell"

@srid
Copy link
Member

srid commented Jun 30, 2023

Could you state the real-world use case where you need this? Let me think about the problem first, before getting to the solution.

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

Is this for #25 (which I didn't entirely understand)?

The problem with this PR is that it will work in some cases (eg: devShell), but not in other cases (eg: nix run). Unless you disable auto wiring (#27), don't add packages.* and say "My process-compose configuration is meant to work only in the devShell"

no, it's not for #25.
The problem I encountered: in monorepo I splitted my flake.nix in different files and placed them in subdirectories. For each project I defined custom processes, and, when suddenly PWD changes, this processes don't start, e.g.:
./deno/web/web-deno.nix

let
  cwd = "./deno/web";
in
process-compose."dev:web-deno".settings.processes.web = {
  command = "exec deno run -A --node-modules-dir npm:vite";
  working_dir = cwd;
};

in root flake.nix:

imports = with inputs; [
  flake-root.flakeModule
  devenv.flakeModule
  treefmt-nix.flakeModule
  process-compose-flake.flakeModule
  ./deno/api/api-deno.nix
  ./deno/web/web-deno.nix

  ./node/web/web-node.nix
];

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

I'm not sure, but maybe in case of nix run it will run by default in place, where flake.nix is located?

@srid
Copy link
Member

srid commented Jun 30, 2023

So it is a devShell only process?

One way to solve this is to use it in combination with https://github.com/Platonic-Systems/mission-control which does support FLAKE_ROOT (unless cdToProjectRoot is set to false). Eg.: https://github.com/nammayatri/nammayatri/blob/493302e23ca968146eaaf90a7b8908057faab688/Backend/nix/scripts.nix#L40-L76

Is this solution sufficient?

@srid
Copy link
Member

srid commented Jun 30, 2023

I'm not sure, but maybe in case of nix run it will run by default in place, where flake.nix is located?

You can run nix run github:MrFoxPro/somerepo from anywhere with no source checked out. If there is a flake package or app, in general we would expect it to work at runtime without requiring the source tree.

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

So it is a devShell only process?

One way to solve this is to use it in combination with https://github.com/Platonic-Systems/mission-control which does support FLAKE_ROOT (unless cdToProjectRoot is set to false). Eg.: https://github.com/nammayatri/nammayatri/blob/493302e23ca968146eaaf90a7b8908057faab688/Backend/nix/scripts.nix#L40-L76

Is this solution sufficient?

Not sure, I took process-compose especially to run multiple commands in parallel.

I'm not sure, but maybe in case of nix run it will run by default in place, where flake.nix is located?

You can run nix run github:MrFoxPro/somerepo from anywhere with no source checked out. If there is a flake package or app, in general we would expect it to work at runtime without requiring the source tree.

Yeah, if you mean that running package that is produced by config.process-compose.<name>.outputs.package, I can also add inPureEvalMode, but.... I'm stuck. Maybe I should use composing devshells, but I can't imagine how to do it when devenv is configured in flake.nix

@srid
Copy link
Member

srid commented Jun 30, 2023

Not sure, I took process-compose especially to run multiple commands in parallel.

It does.

Here's a simple example https://github.com/srid/ema-template/blob/master/flake.nix

Line 55 defines the process-compose group.

        process-compose.run = {
          tui = false;
          settings.processes = {
            haskell.command = "ghcid";
            tailwind.command = "${lib.getExe pkgs.haskellPackages.tailwind} -w -o ./static/tailwind.css './src/**/*.hs'";
          };
        };

Then line 82 uses it from mission-control,

          run = {
            description = "Run the dev server (ghcid + tailwind)";
            exec = config.process-compose.run.outputs.package;
          };

That last part provides , run command in devShell, which you can run from any subdirectory; it will always cd to flake-root.

Let me think about this. Meanwhile, your PR can be improved to directly use the package as well, cf. https://github.com/srid/proc-flake/blob/676b5af467e52c06aa8a9165edb3345c6dbbabde/flake-module.nix#L53-L54

Maybe I should use composing devshells, but I can't imagine how to do it when devenv is configured in flake.nix

Is your flake.nix available somewhere publicly?

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

Yep, so mission-control could be used together with process-compose. Nice!

@srid thanks, I opened here:
https://github.com/MrFoxPro/sferadel/blob/dev/flake.nix
https://github.com/MrFoxPro/sferadel/blob/dev/deno/web/web-deno.nix

@MrFoxPro
Copy link
Author

Nushell supports this: with-env { PWD: "/home/foxpro/craft/sferadel/next/deno" } { ls }
Not sure how to implement it in other shells

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

I mean, how it's supposed to work outside of devshell anyway? Does process-compose use $(dirname "$0") or something to locate its root, and then resolve relative working_dir against that root?

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

I can do this:

${if !lib.inPureEvalMode && config.fromFlakeRoot then "[[ -n $FLAKE_ROOT ]] && cd \"$FLAKE_ROOT\"" else "echo 'Warning: $FLAKE_ROOT' is not defined, script will start agains you current CWD" }

This should work if process-compose resolves it's location itself

@MrFoxPro
Copy link
Author

MrFoxPro commented Jun 30, 2023

Now there is strange thing:

process-compose."dev:web-node".settings = {
  processes.web = {
    command = "ls; echo $PWD; echo $(pwd)";
    working_dir = "./node/web";
  };
};
foxpro:~/craft/sferadel/next$ dev:web-node                                                                                                                                                         
...<shows content of /home/foxpro/craft/sferadel/next/node/web>
[web_1  ] /home/foxpro/craft/sferadel/next <- $PWD
[web_1  ] /home/foxpro/craft/sferadel/next/node/web <- $(pwd)

$PWD now is always same as $FLAKE_ROOT
($pwd) is as working_dir
current directory is as working_dir

@srid
Copy link
Member

srid commented Aug 4, 2023

So it is a devShell only process?

One way to solve this is to use it in combination with https://github.com/Platonic-Systems/mission-control which does support FLAKE_ROOT (unless cdToProjectRoot is set to false). Eg.: https://github.com/nammayatri/nammayatri/blob/493302e23ca968146eaaf90a7b8908057faab688/Backend/nix/scripts.nix#L40-L76

Is this solution sufficient?

Closing per this ^

@srid srid closed this Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants