This commit is contained in:
Amy de Buitléir 2025-10-11 16:32:16 +01:00
parent 134d8da43c
commit 2945a70e3b
6 changed files with 184 additions and 101498 deletions

5813
index.html

File diff suppressed because it is too large Load diff

View file

@ -16,34 +16,18 @@ writing your own flakes. I'll explain the example in some detail.
inputs = {
[.highlight01]#nixpkgs#.url = "github:NixOS/nixpkgs";
[.highlight02]#flake-utils#.url = "github:numtide/flake-utils";
[.highlight03]#..._other dependencies_...# ❶
};
outputs = { self, [.highlight01]#nixpkgs#, [.highlight02]#flake-utils#, [.highlight03]#..._other dependencies_...# ❷ }:
flake-utils.lib.eachDefaultSystem (system: ❸
let
pkgs = import nixpkgs { inherit system; };
python = pkgs.python3;
in
{
devShells = rec {
default = pkgs.mkShell {
packages = [ #_packages needed for development shell_#; ❹ ];
};
};
outputs = { self, [.highlight01]#nixpkgs#, [.highlight03]#..._other dependencies_...# ❷ }: {
packages = rec {
[.highlight04]#myPackageName# = #_package definition_#; ❺
default = [.highlight04]#myPackageName#;
};
devShells = #_shell definitions_#; ❸
packages = #_package definitions_#; ❹
apps = #_app definitions_#; ❺
apps = rec {
[.highlight04]#myPackageName# = flake-utils.lib.mkApp { drv = self.packages.${system}.[.highlight04]#myPackageName#; };
default = [.highlight04]#myPackageName#; ❻
};
}
);
}
----
@ -52,54 +36,84 @@ this part of the flake should be familiar. Remember also that any
dependencies in the input section should also be listed at the beginning
of the outputs section `❷`.
Now it's time to look at the content of the output section. If we want
the package to be available for multiple systems (e.g.,
"`x86_64-linux`", "`aarch64-linux`", "`x86_64-darwin`", and
"`aarch64-darwin`"), we need to define the output for each of those
systems. Often the definitions are identical, apart from the name of the
system. The eachDefaultSystem function `❸` provided by flake-utils allows
us to write a single definition using a variable for the system name.
The function then iterates over all default systems to generate the
outputs for each one.
The `devShells` attribute `❸` specifies the environment that should be
available when doing development on the package.
This includes any tools
(e.g., compilers and other language-specific build tools and packages).
If you don't need a special development environment, you can omit this section.
The `devShells` variable specifies the environment that should be
available when doing development on the package. If you don't need a
special development environment, you can omit this section. At `❹` you
would list any tools (e.g., compilers and language-specific build tools)
you want to have available in a development shell. If the compiler needs
access to language-specific packages, there are Nix functions to assist
with that. These functions are very language-specific, and not always
well-documented. We will see examples for some languages later in the
tutorial. In general, I recommend that you do a web search for
The `packages` attribute `❹` defines the packages that this flake provides.
The definition depends on the programming languages your
software is written in, the build system you use, and more.
There are Nix functions and tools that can simplify much of this, and new,
easier-to-use ones are released regularly.
These functions are very language-specific, and not always well-documented.
We will see examples for some languages later in the tutorial.
In general, I recommend that you do a web search for
"nix _language-name_", and try to find resources that were written or updated
recently.
The `packages` variable defines the packages that this flake provides.
The package definition `❺` depends on the programming languages your
software is written in, the build system you use, and more. There are
Nix functions and tools that can simplify much of this, and new,
easier-to-use ones are released regularly. Again, I recommend that you
do a web search for "nix _language-name_", and try to find resources that
were written or updated recently.
The `apps` variable identifies any applications provided by the flake.
In particular, it identifies the default executable ❻ that `nix run`
The `apps` attribute `❺` identifies any applications provided by the flake.
In particular, it identifies the default executable that `nix run`
will run if you don't specify an app.
If we want the development shell, packages, and apps
to be available for multiple systems
(e.g., `x86_64-linux`, `aarch64-linux`, `x86_64-darwin`, and
`aarch64-darwin`),
we need to provide a definition for each of those systems.
The result _could_ be an outputs section like the one shown below.
[source,subs=quotes]
----
outputs = { self, [.highlight01]#nixpkgs#, [.highlight03]#..._other dependencies_...# ❷ }: {
devShells.x86_64-linux.default = ...;
devShells.aarch64-linux.default = ...;
. . .
packages.x86_64-linux.my-app = ...;
packages.aarch64-linux.my-app = ...;
. . .
apps.x86_64-linux.my-app = ...;
apps.aarch64-linux.my-app = ...;
. . .
apps.x86_64-linux.default = ...;
apps.aarch64-linux.default = ...;
. . .
};
----
You won't see definitions like that in most flakes.
Typically the definitions for each shell, package or app
would be identical, apart from a reference to the system name.
There are techniques and tools that allow you to write a single definition
and use it to automatically generate the definitions for multiple architectures.
We will see some examples of this later in the tutorial.
Below is a list of some functions that are commonly used in
this section.
the output section.
General-purpose::
The standard environment provides
https://noogle.dev/f/pkgs/stdenv/mkDerivation[`mkDerivation`],
which is especially useful for the typical
- https://noogle.dev/f/pkgs/stdenv/mkDerivation[`mkDerivation`]
is especially useful for the typical
`./configure; make; make install` scenario.
It's customisable.
- https://nixos.org/manual/nixpkgs/unstable/#sec-pkgs-mkShell[`mkShell`]
simplifies writing a development shell definition.
- https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeShellApplication[`writeShellApplication`]
creates an executable shell script which also defines the appropriate environment.
Python::
`buildPythonApplication`, `buildPythonPackage`.
- `buildPythonApplication`
- `buildPythonPackage`.
Haskell::
`mkDerivation` (Haskell version, which is a wrapper around the
standard environment version), `developPackage`, `callCabal2Nix`.
- `mkDerivation` (Haskell version, which is a wrapper around the
standard environment version)
- `developPackage`
- `callCabal2Nix`.
[NOTE]

View file

@ -109,3 +109,11 @@ Remove one or more keys and associated values from a set.
nix-repl> builtins.removeAttrs animal [ "age" "species" ]
{ name = "Professor Paws"; }
....
Display an attribute set, including nested sets.
[source]
....
nix-repl> builtins.toJSON animal
"{\"age\":10,\"name\":{\"first\":\"Professor\",\"last\":\"Paws\"},\"species\":\"cat\"}"
....

View file

@ -0,0 +1,77 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"hello-nix": {
"flake": false,
"locked": {
"lastModified": 1757705465,
"narHash": "sha256-sJCQ9+8Dy+QF9ISaupp42+mGbuXtFyqbX85tWzeNPOI=",
"ref": "refs/heads/main",
"rev": "56044f61231c996e4ab795de1da89e5f79db3f4f",
"revCount": 5,
"type": "git",
"url": "https://codeberg.org/mhwombat/hello-nix"
},
"original": {
"type": "git",
"url": "https://codeberg.org/mhwombat/hello-nix"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1760194782,
"narHash": "sha256-fJqK9vyL6Ur2HbtJX1xtBN499+18XLeshXPb/B+Udgw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f84bf9b679e4b8524ab2a90cf854a4f691739457",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"hello-nix": "hello-nix",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,27 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
hello-nix = {
url = "git+https://codeberg.org/mhwombat/hello-nix";
flake = false;
};
};
outputs = { self, nixpkgs, flake-utils, hello-nix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
helloNix = import hello-nix { inherit pkgs; };
in
{
devShells = rec {
default = pkgs.mkShell {
packages = [ helloNix ];
};
};
}
);
}

File diff suppressed because it is too large Load diff