From e223b28e6820dcd9fa5c38ba22de487ada2ca0e6 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Wed, 18 Nov 2020 20:20:27 -0500 Subject: Extend bild to nix targets properly Also had to capitalize some stuff, and move some nix files around and rename the metadata directive from 'exe' to 'out' because that just makes more sense, and fix some compiler errors. But now bild treats both nix and hs files as buildable things. So that's cool. One interesting example is Biz/Pie.{nix,hs} - I can either create a dev build of the hs file with ghc, or I can create a fully-encapsulated nix build. Its nice to have both options because a dev build with ghc takes half the amount of time, and I can rely on my locally cached hi and ho files. I think this shows the power of bild, but also can be a somewhat subtle thing. The issue really is with the separate command calls in nix builds vs dev builds. I figure there are a few ways to fix this: 1. Try to use bild inside the nix rules. That could be interesting, but could also lead to some weird behavior or worm holes forming. 2. Extract the command line invocation into a separate file, some kind of really simple template that gets pulled into both programs. It is important to consider that in the future I might want to have bild do a module-by-module nix build of programs, but I'm not sure how that would effect my choice here. --- Biz/App.hs | 29 +++++++-------- Biz/Bild.hs | 97 ++++++++++++++++++++++++++++++--------------------- Biz/Bild/Rules.nix | 34 ++++++++++-------- Biz/Bild/ShellHook.sh | 2 ++ Biz/Cloud.nix | 27 ++++++++++++++ Biz/Dev.nix | 16 +++++++++ Biz/Ibb/Client.hs | 2 +- Biz/Ibb/Server.hs | 2 +- Biz/Pie.hs | 9 +++++ Biz/Pie.nix | 1 + 10 files changed, 146 insertions(+), 73 deletions(-) create mode 100644 Biz/Cloud.nix create mode 100644 Biz/Dev.nix create mode 100644 Biz/Pie.nix (limited to 'Biz') diff --git a/Biz/App.hs b/Biz/App.hs index 95e7271..d16bba9 100644 --- a/Biz/App.hs +++ b/Biz/App.hs @@ -1,9 +1,10 @@ {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE NoImplicitPrelude #-} -- | General utils for apps -module Biz.App (CSS(..), Manifest(..)) where +module Biz.App (CSS (..), Manifest (..)) where import Alpha import Data.Aeson (ToJSON) @@ -15,10 +16,9 @@ import Network.HTTP.Media ) import Servant -newtype CSS - = CSS - { unCSS :: Text - } +newtype CSS = CSS + { unCSS :: Text + } instance Accept CSS where contentType _ = "text" // "css" /: ("charset", "utf-8") @@ -27,17 +27,14 @@ instance MimeRender CSS Text where mimeRender _ = Lazy.encodeUtf8 . Lazy.fromStrict -- | The manifest describes your app for web app thumbnails, iPhone tiles, etc. -data Manifest - = Manifest - { name :: Text, - short_name :: Text, - start_url :: Text, - display :: Text, - theme_color :: Text, - description :: Text - } +data Manifest = Manifest + { name :: Text, + short_name :: Text, + start_url :: Text, + display :: Text, + theme_color :: Text, + description :: Text + } deriving (Show, Eq, Generic) instance ToJSON Manifest - - diff --git a/Biz/Bild.hs b/Biz/Bild.hs index 92054f9..169fd6f 100644 --- a/Biz/Bild.hs +++ b/Biz/Bild.hs @@ -64,7 +64,7 @@ -- -- > bild A/B.hs -- --- This will build the file at ./A/B.hs, this will translate to something like +-- This will build the file at ./A/B.hs, which translates to something like -- `ghc --make A.B`. -- -- > bild -s @@ -94,16 +94,16 @@ -- -- The output executable is named with: -- --- > -- : exe my-program +-- > -- : out my-program -- -- or -- --- > -- : exe my-ap.js +-- > -- : out my-ap.js -- -- When multiple compilers are possible (e.g. ghc vs ghcjs) we chose ghcjs when --- the target exe ends in .js. +-- the target @out@ ends in .js. -- --- This method of setting metadata in the module comments works pretty well, +-- This method of setting metadata in the module comments works pretty well, -- and really only needs to be done in the entrypoint module anyway. -- -- Local module deps are included by just giving the repo root to the compiler @@ -143,14 +143,14 @@ type Namespace = String type Dep = String -type Exe = String +type Out = String data Compiler = Ghc | Ghcjs | Nix deriving (Show) data Target = Target - { -- | Output executable name - exe :: Exe, + { -- | Output name + out :: Out, -- | Fully qualified namespace partitioned by '.' namespace :: Namespace, -- | Absolute path to file @@ -170,24 +170,30 @@ analyze s = do case File.takeExtension path of ".hs" -> do content <- String.lines Regex.match metaExe |> catMaybes |> head |> require "exe" - let compiler = if ".js" `List.isSuffixOf` exe then Ghcjs else Ghc - return Target { - namespace = require "namespace" - <| path - |> reps root "" - |> File.dropExtension - |> reps "/" "." - |> List.stripPrefix "." - >>= Regex.match metaNamespace, - deps = content /> Regex.match metaDep |> catMaybes, - .. - } - - ".nix" -> return Target { - namespace = s, path = path, deps = [], compiler = Nix, exe = "" - } - + let out = content /> Regex.match metaOut |> catMaybes |> head |> require "out" + let compiler = if ".js" `List.isSuffixOf` out then Ghcjs else Ghc + return + Target + { namespace = + require "namespace" + <| path + |> reps root "" + |> File.dropExtension + |> reps "/" "." + |> List.stripPrefix "." + >>= Regex.match metaNamespace, + deps = content /> Regex.match metaDep |> catMaybes, + .. + } + ".nix" -> + return + Target + { namespace = s, + path = path, + deps = [], + compiler = Nix, + out = "" + } e -> panic <| "bild does not know this extension: " <> Text.pack e build :: Target -> IO () @@ -196,8 +202,8 @@ build Target {..} = do case compiler of Ghc -> do putText <| "bild: ghc: " <> Text.pack namespace - let out = root "_/bild/dev/bin" - Dir.createDirectoryIfMissing True out + let devOut = root "_/bild/dev/bin" + Dir.createDirectoryIfMissing True devOut Process.callProcess "ghc" [ "-Werror", @@ -211,12 +217,12 @@ build Target {..} = do "-main-is", namespace, "-o", - out exe + devOut out ] Ghcjs -> do putText <| "bild: ghcjs: " <> Text.pack namespace - let out = root "_/bild/dev/static" - Dir.createDirectoryIfMissing True out + let devOut = root "_/bild/dev/static" + Dir.createDirectoryIfMissing True devOut Process.callProcess "ghcjs" [ "-Werror", @@ -230,19 +236,30 @@ build Target {..} = do "-main-is", namespace, "-o", - out exe + devOut out ] Nix -> do putText <| "bild: nix: " <> Text.pack namespace cwd <- Dir.getCurrentDirectory - let qualifiedTarget = reps root "" cwd <> namespace + let nixOut = root "_/bild/nix" + Dir.createDirectoryIfMissing True nixOut + let qualifiedTarget = reps root "" cwd namespace Process.callProcess "nix-build" - [ "-o", - root "_/bild/nix" qualifiedTarget, - root "default.nix", - "--attr", - qualifiedTarget + [ path, + "-o", + nixOut qualifiedTarget, + "--arg", + "bild", + "import " <> root + "Biz/Bild/Rules.nix" + <> " { nixpkgs = import " + <> root + "Biz/Bild/Nixpkgs.nix" + <> "; }", + "--arg", + "lib", + "(import " <> root "Biz/Bild/Nixpkgs.nix).lib" ] metaNamespace :: Regex.RE Char Namespace @@ -253,8 +270,8 @@ metaNamespace = name <> Regex.many (Regex.sym '.') <> name metaDep :: Regex.RE Char Dep metaDep = Regex.string "-- : dep " *> Regex.many (Regex.psym Char.isAlpha) -metaExe :: Regex.RE Char Exe -metaExe = Regex.string "-- : exe " *> Regex.many (Regex.psym (/= ' ')) +metaOut :: Regex.RE Char Out +metaOut = Regex.string "-- : out " *> Regex.many (Regex.psym (/= ' ')) require :: Text -> Maybe a -> a require _ (Just x) = x diff --git a/Biz/Bild/Rules.nix b/Biz/Bild/Rules.nix index e76d7a2..2a1a4d1 100644 --- a/Biz/Bild/Rules.nix +++ b/Biz/Bild/Rules.nix @@ -28,9 +28,9 @@ let module = builtins.replaceStrings ["/" ".hs"] ["." ""] relpath; # file contents content = builtins.readFile main; - # search for the ': exe' declaration - exe = builtins.head (lib.lists.flatten (removeNull - (map (builtins.match "^-- : exe ([[:alnum:]._-]*)$") + # search for the ': out' declaration + out = builtins.head (lib.lists.flatten (removeNull + (map (builtins.match "^-- : out ([[:alnum:]._-]*)$") (lines content)))); # collect all of the ': dep' declarations deps = lib.lists.flatten (removeNull @@ -38,7 +38,7 @@ let (lines content))); sysdeps = lib.lists.flatten (removeNull - (map (builtins.match "^-- : sys ([[:alum:]._-]*)$") + (map (builtins.match "^-- : sys ([[:alnum:]._-]*)$") (lines content))); }; @@ -59,16 +59,18 @@ in { ghc = ghc_ data.deps; in stdenv.mkDerivation { name = data.module; - src = ../.; + src = ../../.; nativeBuildInputs = [ ghc ] ++ depsToPackageSet nixpkgs data.sysdeps; strictDeps = true; buildPhase = '' mkdir -p $out/bin # compile with ghc - ${ghc}/bin/ghc -Werror -Weverything -i. \ - --make ${main} \ - -main-is ${data.module} \ - -o $out/bin/${data.exe} + ${ghc}/bin/ghc \ + -Werror \ + -i. \ + --make ${main} \ + -main-is ${data.module} \ + -o $out/bin/${data.out} ''; # the install process was handled above installPhase = "exit 0"; @@ -80,19 +82,21 @@ in { ghcjs = ghcjs_ data.deps; in stdenv.mkDerivation { name = data.module; - src = ../.; + src = ../../.; nativeBuildInputs = [ ghcjs ]; strictDeps = true; buildPhase = '' mkdir -p $out/static # compile with ghcjs - ${ghcjs}/bin/ghcjs -Werror -Weverything -i. \ - --make ${main} \ - -main-is ${data.module} \ - -o ${data.exe} + ${ghcjs}/bin/ghcjs \ + -Werror \ + -i. \ + --make ${main} \ + -main-is ${data.module} \ + -o ${data.out} # optimize js output ${pkgs.closurecompiler}/bin/closure-compiler \ - ${data.exe}/all.js > $out/static/${data.exe} + ${data.out}/all.js > $out/static/${data.out} ''; installPhase = "exit 0"; } // { env = ghcjs; }; diff --git a/Biz/Bild/ShellHook.sh b/Biz/Bild/ShellHook.sh index 89751d3..0fc1781 100644 --- a/Biz/Bild/ShellHook.sh +++ b/Biz/Bild/ShellHook.sh @@ -13,6 +13,8 @@ function help() { echo " ship lint, bild, and push one (or all) namespace(s)" } +alias runghc="runghc --ghc-arg=-i$BIZ_ROOT" + function bild() { runghc Biz.Bild $@ } diff --git a/Biz/Cloud.nix b/Biz/Cloud.nix new file mode 100644 index 0000000..edf8a85 --- /dev/null +++ b/Biz/Cloud.nix @@ -0,0 +1,27 @@ +{ bild }: + +# Cloud infrastructure, always online. Mostly for messaging-related stuff. + +let + nixos-mailserver = let ver = "v2.3.0"; in builtins.fetchTarball { + url = "https://gitlab.com/simple-nixos-mailserver/nixos-mailserver/-/archive/${ver}/nixos-mailserver-${ver}.tar.gz"; + sha256 = "0lpz08qviccvpfws2nm83n7m2r8add2wvfg9bljx9yxx8107r919"; + }; +in +bild.os { + imports = [ + ./OsBase.nix + ./Packages.nix + ./Users.nix + ./Cloud/Chat.nix + ./Cloud/Git.nix + ./Cloud/Hardware.nix + ./Cloud/Mail.nix + ./Cloud/Networking.nix + ./Cloud/Web.nix + ./Cloud/Znc.nix + nixos-mailserver + ]; + networking.hostName = "simatime"; + networking.domain = "simatime.com"; +} diff --git a/Biz/Dev.nix b/Biz/Dev.nix new file mode 100644 index 0000000..a08a8f7 --- /dev/null +++ b/Biz/Dev.nix @@ -0,0 +1,16 @@ +{ bild }: + +# Dev machine for work and building stuff. + +bild.os { + imports = [ + ./OsBase.nix + ./Packages.nix + ./Users.nix + ./Dev/Configuration.nix + ./Dev/Hardware.nix + ]; + networking.hostName = "lithium"; + networking.domain = "dev.simatime.com"; +} + diff --git a/Biz/Ibb/Client.hs b/Biz/Ibb/Client.hs index c3dae4b..d0ed3e3 100644 --- a/Biz/Ibb/Client.hs +++ b/Biz/Ibb/Client.hs @@ -3,7 +3,7 @@ -- | Front-end -- --- : exe ibb.js +-- : out ibb.js -- -- : dep clay -- : dep miso diff --git a/Biz/Ibb/Server.hs b/Biz/Ibb/Server.hs index d7b4969..058bbdc 100644 --- a/Biz/Ibb/Server.hs +++ b/Biz/Ibb/Server.hs @@ -7,7 +7,7 @@ -- | Server -- --- : exe ibb +-- : out ibb -- -- : dep clay -- : dep miso diff --git a/Biz/Pie.hs b/Biz/Pie.hs index 7e1c19e..409f14f 100644 --- a/Biz/Pie.hs +++ b/Biz/Pie.hs @@ -29,6 +29,15 @@ -- - Sean Ellis' question: "How would you feel if you could no longer use this -- product? (a) Very disappointed, (b) somewhat disappointed, (c) not -- disappointed" and then measure the percentage who answer (a). +-- +-- Bild Metadata: +-- +-- : out pie +-- : dep aeson +-- : dep protolude +-- : dep optparse-simple +-- : dep parsec +-- : dep haskeline module Biz.Pie ( main, ) diff --git a/Biz/Pie.nix b/Biz/Pie.nix new file mode 100644 index 0000000..70e2f23 --- /dev/null +++ b/Biz/Pie.nix @@ -0,0 +1 @@ +{ bild }: bild.ghc ./Pie.hs -- cgit v1.2.3