Jump to content

Flake Compat: Difference between revisions

From Official NixOS Wiki
Use channel tarball instead of github flakeref
Toastal (talk | contribs)
Remove “old style” wording as it is stable Nix code.
 
Line 1: Line 1:
[https://github.com/edolstra/flake-compat Flake Compat] is a compatibility layer for Nix [[Flakes]] that lets projects written in the old style Nix (<code>default.nix</code> and <code>shell.nix</code>) use inputs and outputs from flake style Nix setups.
[https://github.com/edolstra/flake-compat Flake Compat] is a compatibility layer for Nix [[Flakes]] that lets projects written with Flakes export <code>default.nix</code> and <code>shell.nix</code> stable Nix consumption.


The flake-compat library downloads the inputs of the flake, pass them to the flake’s <code>outputs</code> function and return an attribute set containing <code>defaultNix</code> and <code>shellNix</code> attributes. The attributes will contain the output attribute set with an extra <code>default</code> attribute pointing to current platform’s <code>defaultPackage</code> (resp. <code>devShell</code> for <code>shellNix</code>).
The flake-compat library downloads the inputs of the flake, pass them to the flake’s <code>outputs</code> function and return an attribute set containing <code>defaultNix</code> and <code>shellNix</code> attributes. The attributes will contain the output attribute set with an extra <code>default</code> attribute pointing to current platform’s <code>defaultPackage</code> (resp. <code>devShell</code> for <code>shellNix</code>).

Latest revision as of 03:35, 10 November 2026

Flake Compat is a compatibility layer for Nix Flakes that lets projects written with Flakes export default.nix and shell.nix stable Nix consumption.

The flake-compat library downloads the inputs of the flake, pass them to the flake’s outputs function and return an attribute set containing defaultNix and shellNix attributes. The attributes will contain the output attribute set with an extra default attribute pointing to current platform’s defaultPackage (resp. devShell for shellNix).

Usage

To integrate flake-compat into a project, place the following into default.nix (for shell.nix, replace defaultNix with shellNix) to use the shim:

❄︎ default.nix
(import
  (
    let
      lock = builtins.fromJSON (builtins.readFile ./flake.lock);
      nodeName = lock.nodes.root.inputs.flake-compat;
    in
    fetchTarball {
      url = lock.nodes.${nodeName}.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.${nodeName}.locked.rev}.tar.gz";
      sha256 = lock.nodes.${nodeName}.locked.narHash;
    }
  )
  { src = ./.; }
).defaultNix

Then in your flake.nix, add flake-compat.url = "github:edolstra/flake-compat"; to your inputs.

Example usage

An example flake.nix that packages a simple shell script:

❄︎ flake.nix
{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.zst";
    flake-compat.url = "github:edolstra/flake-compat";
  };
  outputs = { self, nixpkgs, flake-compat }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in {
      packages.x86_64-linux.hello = pkgs.stdenv.mkDerivation {
        pname = "hello";
        version = "1.0";
        src = ./hello.sh;
        unpackPhase = "true";
        installPhase = ''
          mkdir -p $out/bin
          cp $src $out/bin/hello
          chmod +x $out/bin/hello
        '';
      };
      packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
    };
}

The shell script being packaged:

❄︎ hello.sh
#!/usr/bin/env sh

echo "Hello"

With this setup, running nix run will build and execute the flake as expected. When using flake-compat, it becomes possible to run traditional commands like nix-build which will return a derivation using the flake’s output definitions.