This commit is contained in:
Amy de Buitléir 2025-09-04 20:21:18 +01:00
parent c8fea80c0b
commit eabae3d7c7
14 changed files with 48 additions and 219 deletions

View file

@ -0,0 +1,38 @@
= A Haskell library package in the nixpkgs repo (without a `.cabal` file)
////
$ mkdir tempwork
$ cd tempwork
$ cp ../flake.nix Main.hs .
$ git add flake.nix
$ nix develop
$ git add flake.lock
$ git commit -a -m temp
////
Occasionally you might want to run a short Haskell program that depends on a Haskell library,
but you don't want to bother writing a cabal file.
Example: Access the `containers` package from the `haskellPackages` set in the nixpkgs repo.
[source,nix,linenums]
.flake.nix
....
include::flake.nix[]
....
Here's a short Haskell program that uses it.
[source,haskell,linenums]
.Main.hs
....
include::Main.hs[]
....
Here's a demonstration using the program.
....
$# ../../../../../start-shell nix develop <<EOL
$ runghc Main.hs
$# EOL
....

View file

@ -2,17 +2,19 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
hello-flake.url = "git+https://codeberg.org/mhwombat/hello-flake";
};
outputs = { self, nixpkgs, flake-utils }:
outputs = { self, nixpkgs, flake-utils, hello-flake }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
customGhc = haskellPackages.ghcWithPackages (p: with p; [ containers ]);
in
{
devShells = rec {
default = pkgs.mkShell {
packages = [ pkgs.cowsay ];
buildInputs = [ customGhc ];
};
};
}

View file

@ -1,60 +0,0 @@
{
"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"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1757007868,
"narHash": "sha256-zekS8JUSNEiphLnjWJBFoaX4Kb8GxiiD6FvoKZI+8b0=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "36420cc41abb467f89082432cfe139f5fdbdcea3",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"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

@ -1,25 +0,0 @@
= In `shell.nix`
[source,nix,linenums]
.shell.nix
....
include::shell.nix[]
....
Here's a short Haskell program that uses it.
[source,haskell,linenums]
.Main.hs
....
include::Main.hs[]
....
Here's a demonstration using the program.
....
$# echo '$ nix-shell'
$# nix-shell --run bash <<EOL
$ runghc Main.hs
$# EOL
....

View file

@ -4,25 +4,13 @@ This chapter provides examples of how to use Nix in a variety of scenarios.
Multiple types of recipes are provided are provided for some scenarios;
comparing the different recipes will help you better understand Nix.
- An _"ad hoc" shell_
is useful when you want to quickly create an environment
- _"Ad hoc" shells_
are useful when you want to quickly create an environment
for a one-off task.
- A _traditional nix shell_
is useful when you want to define an environment that you will
use more than once.
- _Nix flakes_
are the recommended approach for development projects.
- You can use `nix-shell` to run scripts in arbitrary languages, providing
the necessary dependencies. This is particularly convenient for
standalone scripts because you don't need to create a repo and write a
separate `flake.nix`.
The script should start with two _"shebang"_ (`#!`) commands. The first
should invoke `nix-shell`. The second should declares the script
interpreter and any dependencies.
are the recommended approach for development projects,
including defining environments that you will use more than once.
include::run/main-generated.adoc[leveloffset=+1]
@ -30,14 +18,12 @@ include::ad-hoc/main-generated.adoc[leveloffset=+1]
include::shebang/main-generated.adoc[leveloffset=+1]
include::devshell/main-generated.adoc[leveloffset=+1]
include::devshell/main.adoc[leveloffset=+1]
include::nixpkgs-pkg/main.adoc[leveloffset=+1]
include::nix-shell/main-generated.adoc[leveloffset=+1]
include::remote-git/main.adoc[leveloffset=+1]
include::remote-git-flake/main.adoc[leveloffset=+1]
include::haskell-nixpkg/main.adoc[leveloffset=+1]
include::haskell-local/main.adoc[leveloffset=+1]

View file

@ -1,10 +0,0 @@
= Access to a top-level package from the Nixpkgs/NixOS repo
Ex: Access two packages from nixpkgs:
hello and cowsay.
include::cli/main-generated.adoc[leveloffset=+1]
include::shell/main-generated.adoc[leveloffset=+1]
include::shebang/main-generated.adoc[leveloffset=+1]

View file

@ -1,25 +0,0 @@
= In a Bash script (flake style)
[source,bash,linenums]
.Script
....
include::my-flake-script.sh[]
....
.Output
....
$# ./my-flake-script.sh
....
= In a Bash script (non-flake style)
[source,bash,linenums]
.Script
....
include::my-script.sh[]
....
.Output
....
$# ./my-script.sh
....

View file

@ -1,24 +0,0 @@
= In `flake.nix`
See the example in <<_introducing_a_dependency>>
where defined a development shell in <<hello-flake-dependency,flake.nix>>
with a dependency on `cowsay` ,
and tested the shell.
= In `shell.nix`
[source,nix,linenums]
.shell.nix
....
include::shell.nix[]
....
Here's a demonstration using the shell.
....
$# echo '$ nix-shell'
$# nix-shell --run bash <<EOL
$ hello
$ cowsay "moo"
$# EOL
....

View file

@ -1,10 +0,0 @@
= Access to a flake defined in a remote git repo
Ex: Access a flake called `hello-flake`,
which is defined in a remote git repo on codeberg.
To use a package from GitHub, GitLab, or any other public platform,
modify the URL.
include::cli/main-generated.adoc[leveloffset=+1]
include::shell/main-generated.adoc[leveloffset=+1]

View file

@ -1,17 +0,0 @@
= In `shell.nix`
[source,nix,linenums]
.shell.nix
....
include::shell.nix[]
....
Here's a demonstration using the shell.
....
$# echo '$ nix-shell'
$# nix-shell --run bash <<EOL
$ hello-flake
$# EOL
....

View file

@ -1,16 +0,0 @@
= In `shell.nix`
[source,nix,linenums]
.shell.nix
....
include::shell.nix[]
....
Here's a demonstration using the shell.
....
$# echo '$ nix-shell'
$# nix-shell --run bash <<EOL
$ hello-nix
$# EOL
....

View file

@ -1,10 +0,0 @@
with (import <nixpkgs> {});
let
hello-nix = import (builtins.fetchGit {
url = "https://codeberg.org/mhwombat/hello-nix";
rev = "aa2c87f8b89578b069b09fdb2be30a0c9d8a77d8";
});
in
mkShell {
buildInputs = [ hello-nix ];
}