cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
3.81k stars 284 forks source link

allowUnfree with flake-parts #521

Open lizelive opened 1 year ago

lizelive commented 1 year ago

383 adds allowUnfree

but new env nix flake init --template github:cachix/devenv#flake-parts

says (inside perSystem) devenv.allowUnfree = true; # wrong

eth3lbert commented 1 year ago

You could try to add following into your flake.nix

#support unfree
_module.args.pkgs = import inputs.nixpkgs {
  inherit system;
  config.allowUnfree = true;
};

something like following:

{
  description = "Description for the project";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    devenv.url = "github:cachix/devenv";
    nix2container.url = "github:nlewo/nix2container";
    nix2container.inputs.nixpkgs.follows = "nixpkgs";
    mk-shell-bin.url = "github:rrbutani/nix-mk-shell-bin";
  };

  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.

        # support unfree
        _module.args.pkgs = import inputs.nixpkgs {
          inherit system;
          config.allowUnfree = true;
        };

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

        devenv.shells.default = {
          name = "my-project";

          imports = [
            # This is just like the imports in devenv.nix.
            # See https:devenv.sh/guides/using-with-flake-parts/#import-a-devenv-module
            # ./devenv-foo.nix
          ];

          # 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.

      };
    };
}
felschr commented 1 year ago

I've tried the _module.args.pkgs approach with permittedInsecurePackages but I still get errors for my insecure package.

eth3lbert commented 1 year ago

Could you post your flake.nix?

felschr commented 1 year ago
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-parts.url = "github:hercules-ci/flake-parts";
    flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
    devenv.url = "github:cachix/devenv";
    treefmt-nix.url = "github:numtide/treefmt-nix";
    treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = inputs@{ nixpkgs, flake-parts, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [ inputs.devenv.flakeModule ./treefmt.nix ];
      systems = [ "x86_64-linux" "x86_64-darwin" ];
      perSystem = { system, config, pkgs, ... }: {
        _module.args.pkgs = import nixpkgs {
          inherit system;
          config.allowUnfree = true;
          config.permittedInsecurePackages = [ "nodejs-16.20.1" ];
        };

        devenv.shells.default = {
          languages.javascript.enable = true;
          languages.javascript.package = pkgs.nodejs-16_x;
        };
      };
    };
}
eth3lbert commented 1 year ago

Could you also post the error? It works on my machine though.

felschr commented 1 year ago

Interesting, it seems that it's working for me now, too, after restarting my shell. Not sure what happened, I did run direnv reload before to make sure the environment is up-to-date. I will make sure to send an update if I encounter the issue again.