sandwich-contexts-0.3.0.1: Contexts for the Sandwich test library
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.Sandwich.Contexts.Nix

Description

This module contains tools for working with Nix, in order to provide Nix-built artifacts to tests.

The Nix package set (Nixpkgs) is one of the largest package sets in the world, and can be a great way to get artifacts reproducibly. All you need is a nix binary available on the PATH.

For example, the following will build a Nix environment based on Nixpkgs release 24.05, containing Emacs and Firefox.

introduceNixContext nixpkgsRelease2405 $
  introduceNixEnvironment ["emacs", "firefox"] $ do
    it "uses the environment" $ do
      envPath <- getContext nixEnvironment

      emacsVersion <- readCreateProcess (proc (envPath </> "bin" </> "emacs") ["--version"]) ""
      info [i|Emacs version: #{emacsVersion}|]

      firefoxVersion <- readCreateProcess (proc (envPath </> "bin" </> "firefox") ["--version"]) ""
      info [i|Firefox version: #{firefoxVersion}|]
Synopsis

Nix contexts

introduceNixContext Source #

Arguments

:: (MonadUnliftIO m, MonadThrow m) 
=> NixpkgsDerivation

Nixpkgs derivation to use

-> SpecFree (LabelValue "nixContext" NixContext :> context) m ()

Child spec

-> SpecFree context m ()

Parent spec

Introduce a NixContext, which contains information about where to find Nix and what version of Nixpkgs to use. This can be leveraged to introduce Nix packages in tests.

The NixContext contains a build cache, so if you build a given derivation more than once in your tests under this node, runs after the first one will be fast.

This function requires a nix binary to be in the PATH and will throw an exception if it isn't found.

introduceNixContext' Source #

Arguments

:: (MonadUnliftIO m, MonadThrow m) 
=> NodeOptions

Custom NodeOptions

-> NixpkgsDerivation

Nixpkgs derivation to use

-> SpecFree (LabelValue "nixContext" NixContext :> context) m ()

Child spec

-> SpecFree context m ()

Parent spec

Same as introduceNixContext, but allows passing custom NodeOptions.

introduceNixContext'' Source #

Arguments

:: (MonadUnliftIO m, MonadThrow m, HasFile context "nix") 
=> NodeOptions

Custom NodeOptions

-> NixpkgsDerivation

Nixpkgs derivation to use

-> SpecFree (LabelValue "nixContext" NixContext :> context) m ()

Child spec

-> SpecFree context m ()

Parent spec

Same as introduceNixContext', but allows specifying the Nix binary via HasFile.

Nix environments

introduceNixEnvironment Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m) 
=> [Text]

List of package names to include in the Nix environment

-> SpecFree (LabelValue "nixEnvironment" FilePath :> context) m () 
-> SpecFree context m () 

Introduce a Nix environment containing the given list of packages, using the current NixContext. These packages are mashed together using the Nix symlinkJoin function. Their binaries will generally be found in "<environment path>/bin".

introduceNixEnvironment' Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m) 
=> NodeOptions

Custom NodeOptions

-> [Text]

List of package names to include in the Nix environment

-> SpecFree (LabelValue "nixEnvironment" FilePath :> context) m () 
-> SpecFree context m () 

Same as introduceNixEnvironment, but allows passing custom NodeOptions.

buildNixSymlinkJoin Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m, MonadLogger m) 
=> [Text]

Package names

-> m FilePath 

Build a Nix environment, as in introduceNixEnvironment.

buildNixSymlinkJoin' Source #

Arguments

:: (HasBaseContextMonad context m, MonadUnliftIO m, MonadLogger m) 
=> NixContext

Nix context

-> [Text]

Package names

-> m FilePath 

Lower-level version of buildNixSymlinkJoin.

buildNixExpression Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m, MonadLogger m) 
=> Text

Nix expression

-> m FilePath 

Build a Nix environment containing the given list of packages, using the current NixContext. These packages are mashed together using the Nix "symlinkJoin" function. Their binaries will generally be found in "<environment path>/bin".

buildNixExpression' Source #

Arguments

:: (HasBaseContextMonad context m, MonadUnliftIO m, MonadLogger m) 
=> NixContext

Nix expression

-> Text 
-> m FilePath 

Lower-level version of buildNixExpression.

buildNixCallPackageDerivation Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m, MonadLogger m, MonadMask m) 
=> Text

Nix derivation

-> m FilePath 

Build a Nix environment expressed as a derivation expecting a list of dependencies, as in the Nix "callPackage" design pattern. I.e. "{ git, gcc, stdenv, ... }: stdenv.mkDerivation {...}"

buildNixCallPackageDerivation' Source #

Arguments

:: (HasBaseContextMonad context m, MonadUnliftIO m, MonadLogger m, MonadMask m) 
=> NixContext

Nix context.

-> Text

Nix derivation.

-> m FilePath 

Lower-level version of buildNixCallPackageDerivation

Nixpkgs releases

nixpkgsRelease2405 :: NixpkgsDerivation Source #

Nixpkgs release 24.05, accessed 11/9/2024. You can compute updated values for this release (or others) by running nix-prefetch-github NixOS nixpkgs --rev release-24.05

nixpkgsRelease2311 :: NixpkgsDerivation Source #

Nixpkgs release 23.11, accessed 2/19/2023. You can compute updated values for this release (or others) by running nix-prefetch-github NixOS nixpkgs --rev release-23.11

Types

type HasNixContext context = HasLabel context "nixContext" NixContext Source #

nixEnvironment :: Label "nixEnvironment" FilePath Source #

type HasNixEnvironment context = HasLabel context "nixEnvironment" FilePath Source #

data NixpkgsDerivation Source #

Constructors

NixpkgsDerivationFetchFromGitHub 

Fields