Skip to content

Commit

Permalink
Replace existing Nix code with a Nix flake (kmonad#551)
Browse files Browse the repository at this point in the history
* Replace existing Nix code with a Nix flake

Using the New Nix Flake
=======================

For Development
---------------

If you use [direnv](https://direnv.net/) then the included top-level
`.envrc` will load the development environment into your shell/editor
for you.

Otherwise, change into the `nix` directory and run `nix develop` and
you will be dropped into a shell with all of the Haskell tools you
expect.

Using the KMonad NixOS Module
-----------------------------

Create your own `flake.nix` that looks something like this:

```nix
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/release-22.05";
    kmonad.url = "github:kmonad/kmonad?dir=nix";
  };

  outputs = { self, nixpkgs, kmonad }: {
    nixosConfigurations.my_host_name = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        kmonad.nixosModules.default
        ./path/to/configuration.nix
      ];
    };
  };
}
```

More information can be found here:

  <https://nixos.wiki/wiki/Flakes>

Building KMonad with Various Versions of GHC
--------------------------------------------

    $ cd nix
    $ nix build .#kmonad-ghc8107
    $ nix build .#kmonad-ghc902
    $ nix build .#kmonad-ghc922

Running the NixOS Virtual Machine Tests
---------------------------------------

    $ cd nix
    $ nix flake check

* nix: Create devShells that mirror the package names

This allows you to drop into a shell where the package, compiler, and
support tools all match.  However, this (currently) only works with
GHC 9.0.2 since various tooling for the other compilers is currently
broken.

Co-authored-by: Peter Jones <[email protected]>
  • Loading branch information
david-janssen and pjones committed May 31, 2022
1 parent 58267c8 commit 0c4de4c
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 462 deletions.
9 changes: 9 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- sh -*-

# Load the devShell from flake.nix:
if type nix &>/dev/null; then
use flake $(pwd)/nix
fi

# Don't let cabal leak state to other projects:
export CABAL_DIR=$(pwd)/.cache/cabal
28 changes: 0 additions & 28 deletions nix/default.nix

This file was deleted.

29 changes: 7 additions & 22 deletions nix/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 109 additions & 23 deletions nix/flake.nix
Original file line number Diff line number Diff line change
@@ -1,33 +1,119 @@
{
description = "An advanced keyboard manager";
description = "KMonad: An advanced keyboard manager.";

inputs = {
nixpkgs.url = "nixpkgs/nixos-21.05";
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};

outputs = { self, nixpkgs, flake-utils }:
let kmonad = import ./default.nix;
in flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system};
in rec {
packages.kmonad = pkgs.haskellPackages.callPackage kmonad { };
outputs = { self, nixpkgs, ... }@inputs:
let
# List of supported systems:
supportedSystems = [ "x86_64-linux" ];

defaultPackage = packages.kmonad;
# List of supported compilers:
supportedCompilers = [
"ghc8107"
"ghc902"
"ghc922"
];

devShell = pkgs.haskellPackages.shellFor {
packages = _: [ packages.kmonad ];
withHoogle = true;
buildInputs = [ pkgs.haskellPackages.cabal-install ];
};
}) // {
nixosModule = ({ ... }: {
nixpkgs.overlays = [ self.overlay ];
imports = [ ./module-base.nix ];
});

overlay = final: prev: {
kmonad = final.haskellPackages.callPackage kmonad { };
# Function to generate a set based on supported systems:
forAllSystems = f:
nixpkgs.lib.genAttrs supportedSystems (system: f system);

# Attribute set of nixpkgs for each system:
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });

# A source file list cleaner for Haskell programs:
haskellSourceFilter = src:
nixpkgs.lib.cleanSourceWith {
inherit src;
filter = name: type:
let baseName = baseNameOf (toString name); in
nixpkgs.lib.cleanSourceFilter name type &&
!(
baseName == "dist-newstyle"
|| nixpkgs.lib.hasPrefix "." baseName
);
};

# A fake version of git which can be used during the build to
# embed the current revision into the binary despite the .git
# directory not being available:
fakeGit = pkgs: pkgs.writeShellScriptBin "git" ''
echo ${self.rev or "dirty"}
'';

# The package derivation:
derivation = pkgs: haskell: (
haskell.callCabal2nix "kmonad" (haskellSourceFilter ../.) { }
).overrideAttrs (orig: {
buildInputs = orig.buildInputs ++ [ (fakeGit pkgs) ];
});
in
{
packages = forAllSystems (system:
let pkgs = nixpkgsFor.${system}; in
{
# The full Haskell package for the default compiler:
kmonad = derivation pkgs pkgs.haskellPackages;

# Just the executables for the default compiler:
default = pkgs.haskell.lib.justStaticExecutables
(derivation pkgs pkgs.haskellPackages);
} // builtins.listToAttrs (map
(compiler: {
name = "kmonad-${compiler}";
value = derivation pkgs pkgs.haskell.packages.${compiler};
})
supportedCompilers));

overlays.default = final: prev: {
kmonad = self.packages.${prev.system}.default;
};

nixosModules.default = { ... }: {
imports = [
./nixos-module.nix
{ nixpkgs.overlays = [ self.overlays.default ]; }
];
};

checks.x86_64-linux.default =
let pkgs = nixpkgsFor.x86_64-linux; in
import ./nixos-test.nix {
inherit pkgs;
module = self.nixosModules.default;
};

devShells = forAllSystems (system:
let shellFor = haskell: name:
haskell.shellFor {
NIX_PATH = "nixpkgs=${nixpkgsFor.${system}.path}";

packages = _: [ self.packages.${system}.${name} ];
withHoogle = true;
buildInputs = [
haskell.cabal-fmt
haskell.cabal-install
haskell.ghcid
haskell.haskell-language-server
haskell.hlint
haskell.ormolu
haskell.stack
];
}; in
{
default = shellFor
nixpkgsFor.${system}.haskellPackages
"kmonad";
} // builtins.listToAttrs (map
(compiler: {
name = "kmonad-${compiler}";
value = shellFor
nixpkgsFor.${system}.haskell.packages.${compiler}
"kmonad-${compiler}";
})
supportedCompilers));
};
}
157 changes: 0 additions & 157 deletions nix/module-base.nix

This file was deleted.

Loading

0 comments on commit 0c4de4c

Please sign in to comment.