Skip to content

Commit

Permalink
Got static compilation working again
Browse files Browse the repository at this point in the history
  • Loading branch information
david-janssen committed Sep 5, 2020
1 parent 9b1a90b commit 5f53c90
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -9,3 +9,5 @@
/tmp
/template/testing.kbd
.stack-work
/bin/
result
16 changes: 10 additions & 6 deletions doc/installation.md
Expand Up @@ -32,12 +32,6 @@ because you installed it yourself or because you are using NixOS, you can build
nix-build nix
```

Currently the process ends with an error, however only after the compilation and
documentation-generation have been completed. If you look through output of
`nix` you will see the paths it mentions and can find both the binary and the
documentation. Perhaps someone more well-versed with `nix` can help me fix this
issue.

Another option with `nix` is to use the `nix-shell` to ensure you have the
correct environment to run `stack` in. You can enter the development environment
using:
Expand All @@ -50,6 +44,16 @@ nix-shell nix/shell.nix
Note: we do also have to compile a little bit of C-code, so make sure `gcc` is
installed as well.

### Static compilation
Every now and then we compile and release a static binary for Linux that should
run on any Linux regardless of the installed libraries (i.e. `ldd` returns `not a
dynamic executable`). If, for some reason, you want to compile a static binary for the state of HEAD yourself, please copy the contents of `./nix/static` into the `kmonad` project root, and then call:

```shell
$(nix-build --no-link -A fullBuildScript)
```


### Windows environment

FIXME: Windows support only for version 0.3.0 and lower. This will be fixed in
Expand Down
7 changes: 4 additions & 3 deletions nix/default.nix
@@ -1,4 +1,5 @@
let
config = import ./config.nix;
pkgs = import (import ./pinned-nixpkgs.nix) { inherit config; };
in with pkgs.haskellPackages; callPackage kmonad { doHaddock=false;}
# config = import ./config.nix;
pkgs = import (import ./pinned-nixpkgs.nix) {};
kmonad = import ./kmonad.nix;
in with pkgs.haskellPackages; callPackage kmonad {}
19 changes: 19 additions & 0 deletions nix/kmonad.nix
@@ -0,0 +1,19 @@
{ mkDerivation, base, cereal, lens, megaparsec, mtl
, optparse-applicative, resourcet, rio, stdenv, time, unix
, unliftio
}:
mkDerivation {
pname = "kmonad";
version = "0.4";
src = ./..;
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [
base cereal lens megaparsec mtl optparse-applicative resourcet rio
time unix unliftio
];
executableHaskellDepends = [ base ];
doHaddock = false;
description = "Advanced keyboard remapping utility";
license = stdenv.lib.licenses.mit;
}
2 changes: 1 addition & 1 deletion nix/static-compilation.nix
Expand Up @@ -25,7 +25,7 @@ let
# Point the script at pwd, using a recent stackage snapshot
stack2nix-script = import "${static-haskell-nix}/static-stack2nix-builder/stack2nix-script.nix" {
inherit pkgs;
stack-project-dir = toString ./.;
stack-project-dir = toString ./..;
hackageSnapshot = "2020-05-24T00:00:00Z";
};

Expand Down
54 changes: 54 additions & 0 deletions nix/static/default.nix
@@ -0,0 +1,54 @@
# Running using: $(nix-build --no-link -A fullBuildScript)
{
stack2nix-output-path ? "custom-stack2nix-output.nix",
nixpkgs ? import <nixpkgs> {},
}:

let

# Define basic configuration
cabalPackageName = "kmonad";
compiler = "ghc865";

# Grab the most recent version of `static-haskell-nix` (at time of writing)
static-haskell-nix = nixpkgs.fetchFromGitHub
{ owner = "nh2";
repo = "static-haskell-nix";
rev = "dbce18f4808d27f6a51ce31585078b49c86bd2b5";
sha256 = "084hxnrywsgb73zr41argdkbhkxzm1rqn058pv1l4cp9g1gjr2rr";
};
# fetchTarball https://github.com/nh2/static-haskell-nix/archive/d1b20f35ec7d3761e59bd323bbe0cca23b3dfc82.tar.gz;

# Import the pkgs provided by `static-haskell-nix`
pkgs = import "${static-haskell-nix}/nixpkgs.nix";

# Point the script at pwd, using a recent stackage snapshot
stack2nix-script = import "${static-haskell-nix}/static-stack2nix-builder/stack2nix-script.nix" {
inherit pkgs;
stack-project-dir = toString ./.;
hackageSnapshot = "2020-05-24T00:00:00Z";
};

# Setup the builder
static-stack2nix-builder = import "${static-haskell-nix}/static-stack2nix-builder/default.nix" {
normalPkgs = pkgs;
inherit cabalPackageName compiler stack2nix-output-path;
# disableOptimization = true; # for compile speed
};

# Full invocation, including pinning `nix` version itself.
fullBuildScript = pkgs.writeScript "stack2nix-and-build-script.sh" ''
#!/usr/bin/env bash
set -eu -o pipefail
STACK2NIX_OUTPUT_PATH=$(${stack2nix-script})
export NIX_PATH=nixpkgs=${pkgs.path}
${pkgs.nix}/bin/nix-build --no-link -A static_package --argstr stack2nix-output-path "$STACK2NIX_OUTPUT_PATH" "$@"
'';

in
{
static_package = static-stack2nix-builder.static_package;
inherit fullBuildScript;
# For debugging:
# inherit static-stack2nix-builder;
}
4 changes: 4 additions & 0 deletions nix/static/stack.yaml
@@ -0,0 +1,4 @@
resolver: lts-14.7
packages:
- .
extra-deps:
1 change: 0 additions & 1 deletion stack.yaml
@@ -1,5 +1,4 @@
resolver: lts-14.7
# resolver: lts-16.12
packages:
- .
extra-deps:

0 comments on commit 5f53c90

Please sign in to comment.