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

Using editablePackageSources with flakes #425

Closed
yrd opened this issue Oct 30, 2021 · 8 comments
Closed

Using editablePackageSources with flakes #425

yrd opened this issue Oct 30, 2021 · 8 comments

Comments

@yrd
Copy link

yrd commented Oct 30, 2021

When using poetry2nix in a flake-based project, editablePackageSources doesn't seem to work as documented:

pkgs.poetry2nix.mkPoetryEnv {
  projectDir = ./.;
  python = pkgs.python39;
  editablePackageSources = {
    myProject = ./.;
  };
}

In this case, the ./. always references the flake's source that was copied into the store (something like /nix/store/p68s64y2g86wb02rj0am4q4wp4cj8mgs-source). I found a workaround by doing something like this:

editablePackageSources = {
  myProject = builtins.getEnv "PWD";
}

This seems to work when running with --impure, but is there a better way to do this without relying on the environment variable?

@hhoeflin
Copy link

hhoeflin commented Dec 8, 2021

I guess fundamentally an editable package will never work with a pure flake, as the whole concept of pureness means it is not editable.

Your pointer helped me. One note, the "PWD" only works when the shell is invoked from the root of the project, so this is a bit of a tripwire. The alternative is to hardcode the path in your home folder into the flake ... also not nice but works as well, so for example

myproject = "${builtins.getEnv "HOME"}/projects/myproject";

@adisbladis
Copy link
Member

This isn't actually an issue in poetry2nix, so I'm gonna close this.

@hhoeflin
Copy link

hhoeflin commented Dec 8, 2021

I agree that this may not be something that can be fixed in poetry2nix but as flakes are supposedly the new standard, some documentation on the README pointing out this issue would be helpful.

@adisbladis
Copy link
Member

adisbladis commented Dec 8, 2021

Flakes are not the new standard, they are an experimental feature.

With that in mind any sharp edges are up to the user of that experimental feature to deal with.

@hhoeflin
Copy link

hhoeflin commented Dec 8, 2021

sorry, sure - it is an experimental feature. I am new to this, so I try to use it almost exclusively. As the flakes are mentioned on the README, I thought it would be good to at least point to some pitfalls. I certainly spent quite a bit of time with this issue as things didn't work as I hoped.

@hhoeflin
Copy link

hhoeflin commented Dec 11, 2021

Wanted to add my current workaround as a solution.

pkgs.poetry2nix.mkPoetryEnv {
  projectDir = ./.;
  python = pkgs.python39;
  editablePackageSources = {
    myProject = if builtins.getEnv "PROJECT_DIR" == "" then ./. else builtins.getEnv "PROJECT_DIR";
  };
}

This way, if the flake is pure, the flake directory is used and copied to the store (being reproducible as flakes intended). If --impure, then it uses the variable PROJECT_DIR if present. nix doesn't pass information as to where on the file system the flake is (and it may be on github etc anyway). But we can work around it, as bash gives this information when invoked. So write a short bash-script named e.g. activate_env.sh:

#! /usr/bin/env bash
export PROJECT_DIR=$(dirname "$(realpath -s "${BASH_SOURCE[0]}")")
nix shell --impure $PROJECT_DIR

When sourced, it will drop you into the shell environment with editable sources. The bash script assumes your project dir is your flake directory as well and the package is set as the default.

@yrd
Copy link
Author

yrd commented Dec 11, 2021

@hhoeflin Thanks! I think that's a nice solution which causes less friction than only PWD while still using flakes (which, yeah, aren't pure anymore but I guess an in-development environment will never be so I think that's okay).

@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/editable-python-environment-with-poetry2nix/44520/2

ianepreston added a commit to ianepreston/stats_can that referenced this issue Jul 21, 2024
ianepreston added a commit to ianepreston/stats_can that referenced this issue Jul 21, 2024
ianepreston added a commit to ianepreston/stats_can that referenced this issue Jul 21, 2024
* fix editable nix

nix-community/poetry2nix#425

* delete cassettes

They've been more trouble than they're worth.
Most of the point of this library is to be an API wrapper
If my tests fail on live runs I want to know

* handle off days

Get changed series returns a 409 (why?!) when it's run on a day with no
updates

* remove pyvcr

You're an API wrapper, test the API

* remove dependency on pytest-recording
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

No branches or pull requests

4 participants