Skip to content

Commit 4dba12c

Browse files
anka-213o1lo01ol1o
andauthored
Add a nix flake for building with nix (#185)
Also based on #165 --------- Co-authored-by: o1lo01ol1o <[email protected]> Co-authored-by: Tim Pierson <[email protected]>
1 parent 5ca230d commit 4dba12c

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ doc/icfp-2012.html
7373
download/*.html
7474
gf-book/index.html
7575
src/www/gf-web-api.html
76+
.devenv
77+
.direnv
78+
result
79+
.vscode
80+
.envrc
81+
.pre-commit-config.yaml

flake.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
4+
systems.url = "github:nix-systems/default";
5+
};
6+
7+
nixConfig = {
8+
# extra-trusted-public-keys =
9+
# "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
10+
# extra-substituters = "https://devenv.cachix.org";
11+
};
12+
13+
outputs = { self, nixpkgs, systems, ... }@inputs:
14+
let forEachSystem = nixpkgs.lib.genAttrs (import systems);
15+
in {
16+
packages = forEachSystem (system:
17+
let
18+
pkgs = nixpkgs.legacyPackages.${system};
19+
haskellPackages = pkgs.haskell.packages.ghc925.override {
20+
overrides = self: _super: {
21+
cgi = pkgs.haskell.lib.unmarkBroken (pkgs.haskell.lib.dontCheck
22+
(self.callHackage "cgi" "3001.5.0.1" { }));
23+
};
24+
};
25+
26+
in {
27+
gf = pkgs.haskell.lib.overrideCabal
28+
(haskellPackages.callCabal2nixWithOptions "gf" self "--flag=-server"
29+
{ }) (_old: {
30+
# Fix utf8 encoding problems
31+
patches = [
32+
# Already applied in master
33+
# (
34+
# pkgs.fetchpatch {
35+
# url = "https://github.com/anka-213/gf-core/commit/6f1ca05fddbcbc860898ddf10a557b513dfafc18.patch";
36+
# sha256 = "17vn3hncxm1dwbgpfmrl6gk6wljz3r28j191lpv5zx741pmzgbnm";
37+
# }
38+
# )
39+
./nix/expose-all.patch
40+
./nix/revert-new-cabal-madness.patch
41+
];
42+
jailbreak = true;
43+
# executableSystemDepends = [
44+
# (pkgs.ncurses.override { enableStatic = true; })
45+
# ];
46+
# executableHaskellDepends = [ ];
47+
});
48+
});
49+
};
50+
}

nix/expose-all.patch

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/gf.cabal b/gf.cabal
2+
index 0076e7638..8d3fe4b49 100644
3+
--- a/gf.cabal
4+
+++ b/gf.cabal
5+
@@ -168,7 +168,6 @@ Library
6+
GF.Text.Lexing
7+
GF.Grammar.Canonical
8+
9+
- other-modules:
10+
GF.Main
11+
GF.Compiler
12+
GF.Interactive

nix/revert-new-cabal-madness.patch

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
commit 45e5473fcd5707af93646d9a116867a4d4e3e9c9
2+
Author: Andreas Källberg <[email protected]>
3+
Date: Mon Oct 10 14:57:12 2022 +0200
4+
5+
Revert "workaround for the Nix madness"
6+
7+
This reverts commit 1294269cd60f3db7b056135104615625baeb528c.
8+
9+
There are easier workarounds, like using
10+
11+
cabal v1-build
12+
13+
etc. instead of just `cabal build`
14+
15+
These changes also broke a whole bunch of other stuff
16+
17+
diff --git a/README.md b/README.md
18+
index ba35795a4..79e6ab68f 100644
19+
--- a/README.md
20+
+++ b/README.md
21+
@@ -38,21 +38,6 @@ or:
22+
```
23+
stack install
24+
```
25+
-Note that if you are unlucky to have Cabal 3.0 or later, then it uses
26+
-the so-called Nix style commands. Using those for GF development is
27+
-a pain. Every time when you change something in the source code, Cabal
28+
-will generate a new folder for GF to look for the GF libraries and
29+
-the GF cloud. Either reinstall everything with every change in the
30+
-compiler, or be sane and stop using cabal-install. Instead you can do:
31+
-```
32+
-runghc Setup.hs configure
33+
-runghc Setup.hs build
34+
-sudo runghc Setup.hs install
35+
-```
36+
-The script will install the GF dependencies globally. The only solution
37+
-to the Nix madness that I found is radical:
38+
-
39+
- "No person, no problem" (Нет человека – нет проблемы).
40+
41+
For more information, including links to precompiled binaries, see the [download page](https://www.grammaticalframework.org/download/index.html).
42+
43+
diff --git a/Setup.hs b/Setup.hs
44+
index 58dc3e0c6..f8309cc00 100644
45+
--- a/Setup.hs
46+
+++ b/Setup.hs
47+
@@ -4,68 +4,42 @@ import Distribution.Simple.LocalBuildInfo(LocalBuildInfo(..),absoluteInstallDirs
48+
import Distribution.Simple.Setup(BuildFlags(..),Flag(..),InstallFlags(..),CopyDest(..),CopyFlags(..),SDistFlags(..))
49+
import Distribution.PackageDescription(PackageDescription(..),emptyHookedBuildInfo)
50+
import Distribution.Simple.BuildPaths(exeExtension)
51+
-import System.Directory
52+
import System.FilePath((</>),(<.>))
53+
-import System.Process
54+
-import Control.Monad(forM_,unless)
55+
-import Control.Exception(bracket_)
56+
-import Data.Char(isSpace)
57+
58+
import WebSetup
59+
60+
+-- | Notice about RGL not built anymore
61+
+noRGLmsg :: IO ()
62+
+noRGLmsg = putStrLn "Notice: the RGL is not built as part of GF anymore. See https://github.com/GrammaticalFramework/gf-rgl"
63+
+
64+
main :: IO ()
65+
main = defaultMainWithHooks simpleUserHooks
66+
- { preConf = gfPreConf
67+
- , preBuild = gfPreBuild
68+
+ { preBuild = gfPreBuild
69+
, postBuild = gfPostBuild
70+
, preInst = gfPreInst
71+
, postInst = gfPostInst
72+
, postCopy = gfPostCopy
73+
}
74+
where
75+
- gfPreConf args flags = do
76+
- pkgs <- fmap (map (dropWhile isSpace) . tail . lines)
77+
- (readProcess "ghc-pkg" ["list"] "")
78+
- forM_ dependencies $ \pkg -> do
79+
- let name = takeWhile (/='/') (drop 36 pkg)
80+
- unless (name `elem` pkgs) $ do
81+
- let fname = name <.> ".tar.gz"
82+
- callProcess "wget" [pkg,"-O",fname]
83+
- callProcess "tar" ["-xzf",fname]
84+
- removeFile fname
85+
- bracket_ (setCurrentDirectory name) (setCurrentDirectory ".." >> removeDirectoryRecursive name) $ do
86+
- exists <- doesFileExist "Setup.hs"
87+
- unless exists $ do
88+
- writeFile "Setup.hs" (unlines [
89+
- "import Distribution.Simple",
90+
- "main = defaultMain"
91+
- ])
92+
- let to_descr = reverse .
93+
- (++) (reverse ".cabal") .
94+
- drop 1 .
95+
- dropWhile (/='-') .
96+
- reverse
97+
- callProcess "wget" [to_descr pkg, "-O", to_descr name]
98+
- callProcess "runghc" ["Setup.hs","configure"]
99+
- callProcess "runghc" ["Setup.hs","build"]
100+
- callProcess "sudo" ["runghc","Setup.hs","install"]
101+
-
102+
- preConf simpleUserHooks args flags
103+
-
104+
- gfPreBuild args = gfPre args . buildDistPref
105+
- gfPreInst args = gfPre args . installDistPref
106+
+ gfPreBuild args = gfPre args . buildDistPref
107+
+ gfPreInst args = gfPre args . installDistPref
108+
109+
gfPre args distFlag = do
110+
return emptyHookedBuildInfo
111+
112+
gfPostBuild args flags pkg lbi = do
113+
+ -- noRGLmsg
114+
let gf = default_gf lbi
115+
buildWeb gf flags (pkg,lbi)
116+
117+
gfPostInst args flags pkg lbi = do
118+
+ -- noRGLmsg
119+
+ saveInstallPath args flags (pkg,lbi)
120+
installWeb (pkg,lbi)
121+
122+
gfPostCopy args flags pkg lbi = do
123+
+ -- noRGLmsg
124+
+ saveCopyPath args flags (pkg,lbi)
125+
copyWeb flags (pkg,lbi)
126+
127+
-- `cabal sdist` will not make a proper dist archive, for that see `make sdist`
128+
@@ -73,16 +47,27 @@ main = defaultMainWithHooks simpleUserHooks
129+
gfSDist pkg lbi hooks flags = do
130+
return ()
131+
132+
-dependencies = [
133+
- "https://hackage.haskell.org/package/utf8-string-1.0.2/utf8-string-1.0.2.tar.gz",
134+
- "https://hackage.haskell.org/package/json-0.10/json-0.10.tar.gz",
135+
- "https://hackage.haskell.org/package/network-bsd-2.8.1.0/network-bsd-2.8.1.0.tar.gz",
136+
- "https://hackage.haskell.org/package/httpd-shed-0.4.1.1/httpd-shed-0.4.1.1.tar.gz",
137+
- "https://hackage.haskell.org/package/exceptions-0.10.5/exceptions-0.10.5.tar.gz",
138+
- "https://hackage.haskell.org/package/stringsearch-0.3.6.6/stringsearch-0.3.6.6.tar.gz",
139+
- "https://hackage.haskell.org/package/multipart-0.2.1/multipart-0.2.1.tar.gz",
140+
- "https://hackage.haskell.org/package/cgi-3001.5.0.0/cgi-3001.5.0.0.tar.gz"
141+
- ]
142+
+saveInstallPath :: [String] -> InstallFlags -> (PackageDescription, LocalBuildInfo) -> IO ()
143+
+saveInstallPath args flags bi = do
144+
+ let
145+
+ dest = NoCopyDest
146+
+ dir = datadir (uncurry absoluteInstallDirs bi dest)
147+
+ writeFile dataDirFile dir
148+
+
149+
+saveCopyPath :: [String] -> CopyFlags -> (PackageDescription, LocalBuildInfo) -> IO ()
150+
+saveCopyPath args flags bi = do
151+
+ let
152+
+ dest = case copyDest flags of
153+
+ NoFlag -> NoCopyDest
154+
+ Flag d -> d
155+
+ dir = datadir (uncurry absoluteInstallDirs bi dest)
156+
+ writeFile dataDirFile dir
157+
+
158+
+-- | Name of file where installation's data directory is recording
159+
+-- This is a last-resort way in which the seprate RGL build script
160+
+-- can determine where to put the compiled RGL files
161+
+dataDirFile :: String
162+
+dataDirFile = "DATA_DIR"
163+
164+
-- | Get path to locally-built gf
165+
default_gf :: LocalBuildInfo -> FilePath
166+
diff --git a/gf.cabal b/gf.cabal
167+
index a055b86be..d00a5b935 100644
168+
--- a/gf.cabal
169+
+++ b/gf.cabal
170+
@@ -2,7 +2,7 @@ name: gf
171+
version: 3.11.0-git
172+
173+
cabal-version: 1.22
174+
-build-type: Simple
175+
+build-type: Custom
176+
license: OtherLicense
177+
license-file: LICENSE
178+
category: Natural Language Processing, Compiler
179+
@@ -44,6 +44,14 @@ data-files:
180+
www/translator/*.css
181+
www/translator/*.js
182+
183+
+custom-setup
184+
+ setup-depends:
185+
+ base >= 4.9.1 && < 4.16,
186+
+ Cabal >= 1.22.0.0,
187+
+ directory >= 1.3.0 && < 1.4,
188+
+ filepath >= 1.4.1 && < 1.5,
189+
+ process >= 1.0.1.1 && < 1.7
190+
+
191+
source-repository head
192+
type: git
193+
location: https://github.com/GrammaticalFramework/gf-core.git

0 commit comments

Comments
 (0)