on master help sign in
Add nix expression for reformatting sources of arbitrary packages

See the description in readme.

commit7a9aab77b34fe0d76c138b7d30d625daf664d4f5 authormrkkrp <mark.karpov@tweag.io> dateSun 14 Jul 2019, 01:01 (7 years ago) parenta444abc
5 files changed+62−10
README.md+12 default.nix+13−2 nix/gitignore/default.nix−8 nix/ormolize/default.nix+19 nix/ormolize/ormolize.sh+18
README.md+12
@@ -64,6 +64,18 @@ formatted output.
6464$ ormolu --mode inplace Module.hs
6565```
6666
67+## Running on Hackage
68+
69+It's possible to try Ormolu on arbitrary packages from Hackage. For that
70+execute:
71+
72+```console
73+$ nix-build -A hackage.<package>
74+```
75+
76+Then inspect `result/log.txt` for possible problems. The derivation will
77+also contain formatted files for inspection.
78+
6779## Contributing
6880
6981See [CONTRIBUTING.md](./CONTRIBUTING.md).
default.nix+13−2
@@ -1,18 +1,29 @@
11let
22 pkgs = import ./nix/nixpkgs;
3− gitignoreSource = import ./nix/gitignore { inherit (pkgs) lib; };
43 compiler = "ghc864";
5− source = gitignoreSource ./.;
4+ source = pkgs.lib.sourceByRegex ./.[
5+ "^.*\.md$"
6+ "^app.*$"
7+ "^data.*$"
8+ "^ormolu.cabal$"
9+ "^src.*$"
10+ "^tests.*$"
11+ ];
612 haskellPackages = pkgs.haskell.packages.${compiler}.override {
713 overrides = (self: super:
814 super // {
915 "ormolu" = super.callCabal2nix "ormolu" source { };
1016 });
1117 };
18+ ormolize = import ./nix/ormolize {
19+ inherit pkgs;
20+ inherit haskellPackages;
21+ };
1222in {
1323 ormolu = haskellPackages.ormolu;
1424 ormolu-shell = haskellPackages.shellFor {
1525 packages = ps: [ ps.ormolu ];
1626 buildInputs = [ pkgs.cabal-install ];
1727 };
28+ hackage = pkgs.lib.mapAttrs ormolize haskellPackages;
1829}
nix/gitignore/default.nixdeleted−8
@@ -1,8 +0,0 @@
1−{ lib }:
2−
3−let
4− rev = "ec5dd0536a5e4c3a99c797b86180f7261197c124";
5− sha256 = "0k2r8y21rn4kr5dmddd3906x0733fs3bb8hzfpabkdav3wcy3klv";
6− url = "https://github.com/hercules-ci/gitignore/archive/${rev}.tar.gz";
7− nixGitIgnore = builtins.fetchTarball { inherit url sha256; };
8−in (import nixGitIgnore { inherit lib; }).gitignoreSource
nix/ormolize/default.nixnew file+19
@@ -0,0 +1,19 @@
1+{ pkgs, haskellPackages }: _: p:
2+ pkgs.stdenv.mkDerivation {
3+ name = p.name + "-ormolized";
4+ src = p.src;
5+ buildInputs = [
6+ haskellPackages.cpphs
7+ haskellPackages.ormolu
8+ pkgs.glibcLocales
9+ ];
10+ LANG = "en_US.UTF-8";
11+ buildPhase = ''
12+ find . -name '*.hs' -exec bash ${./ormolize.sh} {} \; 2> log.txt
13+ '';
14+ installPhase = ''
15+ mkdir "$out"
16+ find . -name '*.hs' -exec cp --parents {} $out \;
17+ cp log.txt $out/log.txt
18+ '';
19+ }
nix/ormolize/ormolize.shnew file+18
@@ -0,0 +1,18 @@
1+#!/usr/bin/env bash
2+
3+set -euo pipefail
4+
5+# drop includes
6+sed -i '/^#include/d' "$1"
7+
8+# drop CPP
9+sed -i '/^{-# LANGUAGE CPP/d' "$1"
10+
11+# deal with CPP in a fairly straightforward way
12+cpphs "$1" --noline > "$1-nocpp" 2> /dev/null
13+
14+# annoyingly, cpphs cannot modify files in place
15+mv "$1-nocpp" "$1"
16+
17+# run ormolu
18+ormolu -m inplace "$1"