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

Outputs spec update makes eachSystemMap powerful #59

Open
aakropotkin opened this issue Apr 16, 2022 · 2 comments
Open

Outputs spec update makes eachSystemMap powerful #59

aakropotkin opened this issue Apr 16, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@aakropotkin
Copy link

aakropotkin commented Apr 16, 2022

Hey I'm not here to really gripe about anything missing, but more to highlight how recent changes to the flake spec in Nix 2.7.0 make eachSystemMap a really elegant function.

If you agree I I'll PR some README updates to highlight the new patterns, since I think it'd be good to put it front and center.

For context Nix 2.7.0 deprecates defaultXXX.${system} in favor of packages.${system}.default making it easier to wrangle defining defaults.
This snippet below is for a trivial project I have with a standard package installation. This flake makes it available to nix-command, or it can be consumed in NixOS/home-manager as a module, or in legacy nix + nixpkgs projects as an overlay.

The boilerplate required previously to handle the defautXXX outputs used to be real ugly.

{
  description = "A handful of useful core utilities and scripts for Linux";

  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs/nixpkgs-unstable;
    utils.url   = github:numtide/flake-utils;
  };

  outputs = { self, nixpkgs, utils }:
    let
      eachDefaultSystemMap = utils.lib.eachSystemMap utils.lib.defaultSystems;
    in {
      packages = eachDefaultSystemMap ( system: rec {
        ak-core =
          ( import nixpkgs { inherit system; } ).callPackage ./default.nix {};
        default = ak-core;
      } );

      overlays.ak-core = final: prev: {
        inherit (self.packages.${final.system}) ak-core;
      };
      overlays.default = self.overlays.ak-core;

      nixosModules.ak-core = { ... }: {
        nixpkgs.overlays = self.overlays.ak-core;
      };
      nixosModule = self.nixosModules.ak-core;
    };
}
@aakropotkin
Copy link
Author

aakropotkin commented May 15, 2022

A second useful example that is backwards compatible with Nix versions < 2.7:

{
  description = "My DWM Config";

  inputs.utils.url = github:numtide/flake-utils;
  inputs.utils.inputs.nixpkgs.follows = "nixpkgs";

  outputs = { self, nixpkgs, utils }:
    let defaultSystemsMap = utils.lib.eachSystemMap utils.lib.defaultSystems;
    in {
      packages = defaultSystemsMap ( system:
        let pkgsFor = import nixpkgs { inherit system; };
        in rec {
          ak-dwm = pkgsFor.stdenv.mkDerivation {
            pname = "ak-dwm";
            version = "0.0.1";
            src  = self;
            depsTargetTarget = with pkgsFor.xorg; [
              libX11 libXinerama libXft
            ];
            prePatch = ''
              sed -i "s,/usr/local,$out," config.mk
            '';
          };
        default = ak-dwm;
      } );
    } // ( if ( ( builtins.compareVersions __nixVersion "2.7.0" ) <= 0 )
           then {
             defaultPackage =
               defaultSystemsMap ( system: self.packages.${system}.default );
           } else {} );
}

This passes all of the following:

nix shell github:NixOS/nix/2.7-maintanence#nix -c nix flake check --impure;
nix shell github:NixOS/nix/2.6-maintanence#nix -c nix flake check --impure;
nix shell github:NixOS/nix/2.7-maintanence#nix -c nix build --impure;
nix shell github:NixOS/nix/2.6-maintanence#nix -c nix build --impure;

Notably it avoids depreciation warnings when run on 2.7 or later which would normally appear for nix flake check in 2.7 or later.

@zimbatm
Copy link
Member

zimbatm commented May 17, 2022

Thanks, I think this will be useful for people who search. (edited your messages to add highlighting on the nix code)

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