DeterminateSystems/zero-to-nix

Explain how to include a flake in the output

a-h opened this issue · 0 comments

a-h commented

To extend the current behaviour to include external flake, I had to do a couple of things:

{
  description = "api";

  # Add my flake input here.
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
    xc.url = "github:joerdav/xc";
  };

  outputs = { self, nixpkgs, xc }:
    let
      # Systems supported
      allSystems = [
        "x86_64-linux" # 64-bit Intel/AMD Linux
        "aarch64-linux" # 64-bit ARM Linux
        "x86_64-darwin" # 64-bit Intel macOS
        "aarch64-darwin" # 64-bit ARM macOS
      ];

      # Helper to provide system-specific attributes.
      # Note, adding `system` to the output here.
      forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
        system = system;
        pkgs = import nixpkgs {
          inherit system;
        };
      });
    in
    {
      # `nix build` builds the app.
      packages = forAllSystems ({ system, pkgs }: {
        default = pkgs.buildGo121Module {
          name = "api";
          src = ./.;
          subPackages = [ "./app" ];
          vendorHash = "sha256-TvncLmPfELnkFnp88xiFXWww7pniZb5YpTeW6oRiweA=";
          proxyVendor = true;
          postInstall = ''
            mv $out/bin/app $out/bin/api
          '';
        };
      });
      # `nix develop` provides a shell containing required tools.
      # Note the use of the `system` attribute that is now output by the `forAllSystems` function as
      # an input to the function. This lets the flake input be referenced.
      devShell = forAllSystems ({ system, pkgs }:
        pkgs.mkShell {
          buildInputs = [
            pkgs.go_1_21
            pkgs.nats-server
            pkgs.minio
            pkgs.minio-client
            pkgs.minio-certgen
            xc.packages.${system}.xc
          ];
        });
    };
}

I don't know if there's a better way of doing this, but I noticed that zero-to-nix doesn't seem to include an example of doing this sort of thing.

If there's appetite, I can do a PR?