Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flake for nix users #202

Merged
merged 11 commits into from
Sep 8, 2021
Merged
22 changes: 20 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
cabal: ["latest"]
ghc:
- "8.8.4"
- "8.10.4"
- "8.10.7"

steps:
- uses: actions/checkout@v2
Expand Down Expand Up @@ -95,8 +95,26 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v12
- uses: cachix/install-nix-action@v13
with:
nix_path: nixpkgs=channel:nixos-unstable
- run: nix-build
- run: nix-build shell.nix

nix-flakes:
name: nix flakes / ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v13
with:
# This is taken from https://github.com/ngi-nix/summer-of-nix/blob/master/2021/snippets-and-templates/github-actions.yml
install_url: https://github.com/numtide/nix-unstable-installer/releases/download/nix-2.4pre20210604_8e6ee1b/install
extra_nix_config: |
experimental-features = nix-command flakes
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
nix_path: nixpkgs=channel:nixos-unstable
- run: nix flake show
- run: nix build
- run: nix flake check
- run: nix build .#devShell.x86_64-linux
111 changes: 23 additions & 88 deletions .nix-helpers/nixpkgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,107 +19,42 @@
# however this will mean the environment will need to be rebuilt every
# time the termonad source changes.
indexTermonad ? false
# Extra Haskell packages that will be visible by Termonad when it compiles
# itself. See ./termonad-with-packages.nix for an example of how to use
# this.
, extraHaskellPackages ? null
}:

let
flake-lock = builtins.fromJSON (builtins.readFile ../flake.lock);

nixpkgsSrc =
if isNull nixpkgs
then
builtins.fetchTarball {
# Recent version of nixpkgs master as of 2021-05-09 which uses nightly-2020-05-07.
url = "https://github.com/NixOS/nixpkgs/archive/1c16013bd6e94da748b41cc123c6b509a23eb440.tar.gz";
sha256 = "1m2wif0qnci0q14plbqlb95vx214pxqgw5li86lyw6hsiy7y3zfn";
url = "https://github.com/NixOS/nixpkgs/archive/${flake-lock.nodes.nixpkgs.locked.rev}.tar.gz";
sha256 = flake-lock.nodes.nixpkgs.locked.narHash;
}
else nixpkgs;

compilerVersion = if isNull compiler then "ghc8104" else compiler;

# An overlay that adds termonad to all haskell package sets.
haskellPackagesOverlay = self: super: {
haskell = super.haskell // {
packageOverrides = hself: hsuper:
super.haskell.packageOverrides hself hsuper // {
termonad =
let
filesToIgnore = [
".git"
".nix-helpers"
"result"
".stack-work"
"stack.yaml"
"stack-nightly.yaml"
".travis.yml"
];

src =
builtins.filterSource
(path: type: with self.lib;
! elem (baseNameOf path) filesToIgnore &&
! any (flip hasPrefix (baseNameOf path)) [ "dist" ".ghc" ]
)
./..;
haskellPackagesOverlays = import ./overlays.nix;

extraCabal2nixOptions =
self.lib.optionalString buildExamples "-fbuildexamples";
# This overlays sets some of the options use we at development time.
termonadOptionsOverlay = self: super: {
termonadCompilerVersion =
if isNull compiler then super.termonadCompilerVersion else compiler;

termonadDrv =
hself.callCabal2nixWithOptions
"termonad"
src
extraCabal2nixOptions
{
# There are Haskell packages called gtk3 and pcre2, which
# makes these system dependencies not able to be resolved
# correctly.
inherit (self) gtk3 pcre2;
vte_291 = self.vte;
};
in
termonadDrv;
};
};
termonadBuildExamples = buildExamples;

# A Haskell package set where we know the GHC version works to compile
# Termonad. This is basically just a shortcut so that other Nix files
# don't need to figure out the correct compiler version to use when it is
# not given by the user.
termonadKnownWorkingHaskellPkgSet = self.haskell.packages.${compilerVersion};
termonadIndexTermonad = indexTermonad;

# This is a shell environment for hacking on Termonad with cabal. See the
# top-level shell.nix for an explanation.
termonadShell =
let
# Nix-shell environment for hacking on termonad.
termonadEnv = self.termonadKnownWorkingHaskellPkgSet.termonad.env;

# Build tools that are nice to have. It is okay to get Haskell build tools
# from any Haskell package set, since they do not depend on the GHC version
# we are using. We get these from the normal haskellPackages pkg set because
# then they don't have to be compiled from scratch.
convenientNativeBuildTools = [
self.cabal-install
self.gnome3.glade
self.haskellPackages.ghcid
self.hlint
];
in

if indexTermonad
then
termonadEnv.overrideAttrs (oldAttrs: {
nativeBuildInputs =
let
ghcEnvWithTermonad =
self.termonadKnownWorkingHaskellPkgSet.ghcWithHoogle (hpkgs: [ hpkgs.termonad ]);
in
oldAttrs.nativeBuildInputs ++ convenientNativeBuildTools ++ [ ghcEnvWithTermonad ];
})
else
self.termonadKnownWorkingHaskellPkgSet.shellFor {
withHoogle = true;
packages = hpkgs: [ hpkgs.termonad ];
nativeBuildInputs = termonadEnv.nativeBuildInputs ++ convenientNativeBuildTools;
};
termonadExtraHaskellPackages =
if isNull extraHaskellPackages then super.termonadExtraHaskellPackages else extraHaskellPackages;
};

in import nixpkgsSrc { overlays = [ haskellPackagesOverlay ] ++ additionalOverlays; }
in

import nixpkgsSrc {
overlays =
haskellPackagesOverlays ++ [ termonadOptionsOverlay ] ++ additionalOverlays;
}
154 changes: 154 additions & 0 deletions .nix-helpers/overlays.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@

let
# An overlay that adds termonad to all haskell package sets.
haskellPackagesOverlay = self: super: {
haskell = super.haskell // {
packageOverrides = hself: hsuper:
super.haskell.packageOverrides hself hsuper // {
termonad =
let
filesToIgnore = [
"default.nix"
"flake.nix"
"flake.lock"
".git"
".nix-helpers"
"result"
"shell.nix"
".stack-work"
"stack.yaml"
"stack-nightly.yaml"
".travis.yml"
];

src =
builtins.path {
# Naming this path makes sure that people will get the same
# hash even if they checkout the termonad repo into a
# directory called something else.
name = "termonad-src";
path = ./..;
filter = path: type:
with self.lib;
! elem (baseNameOf path) filesToIgnore &&
! any (flip hasPrefix (baseNameOf path)) [ "dist" ".ghc" ];
};

extraCabal2nixOptions =
self.lib.optionalString self.termonadBuildExamples "-fbuildexamples";

termonadDrv =
hself.callCabal2nixWithOptions
"termonad"
src
extraCabal2nixOptions
{
# There are Haskell packages called gtk3 and pcre2, which
# makes these system dependencies not able to be resolved
# correctly.
inherit (self) gtk3 pcre2;
vte_291 = self.vte;
};
in
termonadDrv;
};
};

# This defines which compiler version is used to build Termonad.
#
# Either this, or termonadKnownWorkingHaskellPkgSet can be changed in an overlay
# if you want to use a different GHC to build Termonad.
termonadCompilerVersion = "ghc8107";

# A Haskell package set where we know the GHC version works to compile
# Termonad. This is basically just a shortcut so that other Nix files
# don't need to figure out the correct compiler version to use when it is
# not given by the user.
termonadKnownWorkingHaskellPkgSet = self.haskell.packages.${self.termonadCompilerVersion};

# See ./nixpkgs.nix for an explanation of that this does.
termonadBuildExamples = false;

# See ./nixpkgs.nix for an explanation of that this does.
termonadIndexTermonad = false;

# This is a shell environment for hacking on Termonad with cabal. See the
# top-level shell.nix for an explanation.
termonadShell =
let
# Nix-shell environment for hacking on termonad.
termonadEnv = self.termonadKnownWorkingHaskellPkgSet.termonad.env;

# Build tools that are nice to have. It is okay to get Haskell build tools
# from any Haskell package set, since they do not depend on the GHC version
# we are using. We get these from the normal haskellPackages pkg set because
# then they don't have to be compiled from scratch.
convenientNativeBuildTools = [
self.cabal-install
self.gnome3.glade
self.haskellPackages.ghcid
self.hlint
];
in

if self.termonadIndexTermonad
then
termonadEnv.overrideAttrs (oldAttrs: {
nativeBuildInputs =
let
ghcEnvWithTermonad =
self.termonadKnownWorkingHaskellPkgSet.ghcWithHoogle (hpkgs: [ hpkgs.termonad ]);
in
oldAttrs.nativeBuildInputs ++ convenientNativeBuildTools ++ [ ghcEnvWithTermonad ];
})
else
self.termonadKnownWorkingHaskellPkgSet.shellFor {
withHoogle = true;
packages = hpkgs: [ hpkgs.termonad ];
nativeBuildInputs = termonadEnv.nativeBuildInputs ++ convenientNativeBuildTools;
};

# Default Haskell packages that you can use in your Termonad configuration.
# This is only used if the user doesn't specify the extraHaskellPackages
# option.
termonadExtraHaskellPackages = hpkgs: with hpkgs; [
colour
lens
];

termonad-with-packages =
let
# GHC environment that has termonad available, as well as the packages
# specified above in extraHaskellPackages.
env =
self.termonadKnownWorkingHaskellPkgSet.ghcWithPackages
(hpkgs: [ hpkgs.termonad ] ++ self.termonadExtraHaskellPackages hpkgs);
in
self.stdenv.mkDerivation {
name = "termonad-with-packages-ghc-${env.version}";
buildInputs = [
self.gdk_pixbuf
self.gnome3.adwaita-icon-theme
self.hicolor-icon-theme
];
nativeBuildInputs = [ self.wrapGAppsHook ];
dontBuild = true;
unpackPhase = ":";
# Using installPhase instead of buildCommand was recommended here:
# https://github.com/cdepillabout/termonad/pull/109
installPhase = ''
runHook preInstall
mkdir -p $out/bin
ln -sf ${env}/bin/termonad $out/bin/termonad
gappsWrapperArgs+=(
--set NIX_GHC "${env}/bin/ghc"
)
runHook postInstall
'';
preferLocalBuild = true;
allowSubstitutes = false;
};
};
in

[ haskellPackagesOverlay ]
43 changes: 5 additions & 38 deletions .nix-helpers/termonad-with-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -87,50 +87,17 @@
# }
# ```

let
# Default Haskell packages that you can use in your Termonad configuration.
# This is only used if the user doesn't specify the extraHaskellPackages
# option.
defaultPackages = hpkgs: with hpkgs; [
colour
lens
];
in

{ extraHaskellPackages ? defaultPackages
{ extraHaskellPackages ? null
, nixpkgs ? null
, additionalOverlays ? []
, compiler ? null
, buildExamples ? false
}@args:

with (import ./nixpkgs.nix { inherit compiler nixpkgs additionalOverlays buildExamples; });

let
# GHC environment that has termonad available, as well as the packages
# specified above in extraHaskellPackages.
env =
termonadKnownWorkingHaskellPkgSet.ghcWithPackages
(hpkgs: [ hpkgs.termonad ] ++ extraHaskellPackages hpkgs);
pkgs = import ./nixpkgs.nix {
inherit compiler nixpkgs additionalOverlays buildExamples extraHaskellPackages;
};
in

stdenv.mkDerivation {
name = "termonad-with-packages-ghc-${env.version}";
buildInputs = [ gdk_pixbuf gnome3.adwaita-icon-theme hicolor-icon-theme ];
nativeBuildInputs = [ wrapGAppsHook ];
dontBuild = true;
unpackPhase = ":";
# Using installPhase instead of buildCommand was recommended here:
# https://github.com/cdepillabout/termonad/pull/109
installPhase = ''
runHook preInstall
mkdir -p $out/bin
ln -sf ${env}/bin/termonad $out/bin/termonad
gappsWrapperArgs+=(
--set NIX_GHC "${env}/bin/ghc"
)
runHook postInstall
'';
preferLocalBuild = true;
allowSubstitutes = false;
}
pkgs.termonad-with-packages
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
, buildExamples ? false
}@args:

import .nix-helpers/termonad-with-packages.nix args
(import .nix-helpers/nixpkgs.nix args).termonad-with-packages

Loading