Flake Compat: Difference between revisions
Create flake-compat page, move some content from the main flake page |
m Add category flakes |
||
Line 72: | Line 72: | ||
With this setup, running <code>nix run</code> will build and execute the flake as expected. When using flake-compat, it becomes possible to run traditional commands like <code>nix-build</code> which will return a derivation using the flake’s output definitions. | With this setup, running <code>nix run</code> will build and execute the flake as expected. When using flake-compat, it becomes possible to run traditional commands like <code>nix-build</code> which will return a derivation using the flake’s output definitions. | ||
[[Category:Flakes]] |
Latest revision as of 18:03, 1 June 2025
Flake Compat is a compatibility layer for Nix Flakes that lets projects written in the old style Nix (default.nix
and shell.nix
) use inputs and outputs from flake style Nix setups.
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:
(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:
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
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:
#!/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.