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

flake.nix: Add flakeModule #360

Merged
merged 4 commits into from
Mar 18, 2023
Merged

Conversation

roberth
Copy link
Contributor

@roberth roberth commented Feb 5, 2023

Makes devenv usable for flake-parts users.

Comment on lines +1 to +7
`devenv` can be used without the `devenv` CLI by integrating into [Nix Flakes](https://www.tweag.io/blog/2020-05-25-flakes/), if you're more familiar with the Nix language/ecosystem.

Some usecases for using devenv configuration inside flakes is for projects that want to define other Nix flake features, apart from the development shell.
These include a Nix package for the project, NixOS and home-manager modules related to the project.
Usually you want to use the same lock file for the development shell as well as the Nix package and others, so that everything is based on the same nixpkgs.

A Nix flake includes the inputs from `devenv.yaml` as well as the devenv configuration that you'd usually find in `devenv.nix`. `flake.lock` is the lock file for Nix flakes, the equivalent to `devenv.lock`.
Copy link

Choose a reason for hiding this comment

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

When using devenv via the flake module, assuming that the devenv CLI isn't available, how would I run processes/services defined in configuration?

Copy link

Choose a reason for hiding this comment

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

On that note, it would be pretty amazing to expose the process runners and/or the direct commands they execute via flake.apps

Copy link

Choose a reason for hiding this comment

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

assuming that the devenv CLI isn't available

Bad assumption -- I just checked out this branch (after rebasing onto main) and I see that the CLI is available. However, when I run devenv up:

✦ ❯ devenv up
File devenv.nix does not exist. To get started, run:

  $ devenv init

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I accidentally shadowed the shim by adding the whole devenv package to the packages. That was old code from before the shim existed. Now the module is on par with the existing flake integration.

@terlar
Copy link

terlar commented Feb 22, 2023

How to propagate the pre-commit-hooks.nix flake-parts integration into devenv, I tried to do something like this:

{
  perSystem = {config, ...}: {
    devenv.shells.default = {
      pre-commit = {
        inherit (config.pre-commit.settings) hooks settings;
      };
    };
  };
}

But all the settings end up being repeated/merged, e.g. the generated YAML file:

...
        {
          "entry": "/nix/store/d1x69xig1bwbk8xpn35zw6lcizdwnyhh-statix-0.5.6/bin/statix check -o errfmt /nix/store/d1x69xig1bwbk8xpn35zw6lcizdwnyhh-statix-0.5.6/bin/statix check -o errfmt ",
          "exclude": "^$^$",
          "files": "\\.nix$\\.nix$",
          "id": "statix",
          "language": "systemsystem",
          "name": "statixstatix",
          "pass_filenames": false,
          "stages": [
            "commit",
            "commit"
          ],
          "types": [
            "file",
            "file"
          ],
          "types_or": []
        },
...

@terlar
Copy link

terlar commented Feb 22, 2023

For now my workaround was to not use the integration, but instead just add the hook via a script:

{
  perSystem = {config, ...}: {
    devenv.shells.default = {
      enterShell = config.pre-commit.installationScript;
    };
  };
}

@roberth
Copy link
Contributor Author

roberth commented Mar 8, 2023

Just did a trivial rebase to update the CI config and hopefully fix the CI, but looks like it needs to be approved.

nix flake --impure
```

In normal `nix flake` projects, `--impure` is not needed. When using `devenv` in your flake, you _do_ need this option.
Copy link

Choose a reason for hiding this comment

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

Out of curiosity, why does devenv require --impure?

Copy link

Choose a reason for hiding this comment

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

I've also been curiously wondering about this.

Copy link

Choose a reason for hiding this comment

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

Related: #330

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unclear to me too, but this is just inherited from the existing flake integration.

@nazarewk
Copy link

nazarewk commented Mar 17, 2023

I'm getting same error as reported in #495 with latest commit of this PR, here is my complete flake.nix:

error: Function called without required argument "system" at /nix/store/l5m6bhcq0g2rz0pfkqqrk4ix72grs0s8-source/src/modules/mkNakedShell.nix:27, did you mean "mystem", "systemc" or "systemd"?
{
  description = "Description for the project";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    # devenv.url = "github:cachix/devenv/latest";
    # see https://github.com/cachix/devenv/pull/360
    devenv.url = "github:cachix/devenv/9f761946802870fcbfd5710cd722b568971e057a";
  };

  outputs = inputs@{ flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [
        inputs.devenv.flakeModule
      ];
      systems = [ "x86_64-linux" "i686-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];

      perSystem = { config, self', inputs', pkgs, system, ... }: {
        # Per-system attributes can be defined here. The self' and inputs'
        # module parameters provide easy access to attributes of the same
        # system.

        # Equivalent to  inputs'.nixpkgs.legacyPackages.hello;
        packages.default = pkgs.hello;

        devenv.shells.default = {
          # https://devenv.sh/reference/options/
          packages = [ config.packages.default ];

          enterShell = ''
            hello
          '';
        };

      };
      flake = {
        # The usual flake attributes can be defined here, including system-
        # agnostic ones like nixosModule and system-enumerating ones, although
        # those are more easily expressed in perSystem.

      };
    };
}

@roberth
Copy link
Contributor Author

roberth commented Mar 17, 2023

@nazarewk How did you invoke it?

nix build and nix develop work for me. The error seems unrelated.

@nazarewk
Copy link

nazarewk commented Mar 17, 2023

@nazarewk How did you invoke it?

nix build and nix develop work for me. The error seems unrelated.

The error is indeed unrelated to your changes, updated my issue #495 to reflect that.

I just tried direnv reload

update: nix develop work just fine

@roberth
Copy link
Contributor Author

roberth commented Mar 17, 2023

Rebased, tests pass locally (workflow needs approval), added the containers as packages.

@domenkozar wdyt?

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.

6 participants