aboutsummaryrefslogblamecommitdiff
path: root/default.nix
blob: 3f814b97b29a8b4986b6815da74cd645eb382840 (plain) (tree)
1
2
3
4
5
6
7
8
9
   






                                                                    
                                                      

                                                                  






                                                                               
                 
                                                                         




                                                            


                                       
                              
                                      
                   



                                                       

                       
                  
                                                
                                       
                                                  
                     
                                                                         



                    
                                                       





                                                                          
                                                   
                                           
                                                    





                        
                                                           






                                           



                                                      












                                                                









                                                                    
 
                                   
                                                                                                                                                                                           




                                                      


                  
                                               
                                      
                   



                                                                               
 
                                                                                                   
 
                        
 



                          
    
                                                 





                                    
                        
                            
      
                   





                                                   
       
     
                       


                           
                                                                  
            
                                                       










                                                                     
                                                                     
                                                           
     
 
let
  pkgsUnstable = import <nixpkgs> { };
  pkgsPinned = import (pkgsUnstable.fetchzip {
    url = "https://github.com/NixOS/nixpkgs/archive/18.03.zip";
    sha256 = "0hk4y2vkgm1qadpsm4b0q1vxq889jhxzjx3ragybrlwwg54mzp4f";
  }) { };
  pkgs = pkgsUnstable;

  rootSrc = pkgs.nix-gitignore.gitignoreSource [] ./.;
  nixfmt = import (builtins.fetchTarball
  "https://github.com/serokell/nixfmt/archive/master.tar.gz") { };

  # Taken from:
  # http://www.cs.yale.edu/homes/lucas.paul/posts/2017-04-10-hakyll-on-nix.html
  websiteBuilder = pkgs.stdenv.mkDerivation {
    name = "website-builder";
    src = ./hakyll;
    phases = "unpackPhase buildPhase";
    buildInputs =
    [(pkgsPinned.haskellPackages.ghcWithPackages (p: with p; [hakyll]))];
    buildPhase = ''
      mkdir -p $out/bin
      ghc -O2 -dynamic --make Main.hs -o $out/bin/build-site
    '';
  };
  baseTask = pkgs.stdenv.mkDerivation {
    name = "website-task";
    src = rootSrc;
    buildInputs = [pkgs.perl];
    phases = "unpackPhase buildPhase";
    buildPhase = ''
      echo "ERROR: base task buildPhase not overriden."
      exit 1
    '';
  };
in with pkgs;
with pkgs.stdenv; rec {
  subtasks = rec {
    fixme = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-fixme";
      buildInputs = baseAttrs.buildInputs ++ [ag];
      buildPhase = ''
        ag FIXME --ignore default.nix --ignore pastebin/skeleton.org || {
          touch $out
        }
      '';
    });
    perlPodCheck = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-perl-podcheck";
      buildPhase = ''
        podchecker website pastebin/website-pastebin slides/website-slides
        touch $out
      '';
    });
    batsTest = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-bats-test";
      buildInputs = baseAttrs.buildInputs ++ [bats];
      buildPhase = ''
        patchShebangs .
        ./t/website.bats
        touch $out
      '';
    });
    perlInternalTest = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-perl-test";
      buildPhase = ''
        patchShebangs .
        ./website test
        touch $out
      '';
    });
    formatNix = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-nixfmt";
      buildInputs = baseAttrs.buildInputs ++ [nixfmt];
      buildPhase = ''
        diff <(nixfmt < default.nix) default.nix || {
          echo "The default.nix is unformatted. To fix it, run:"
          echo "  nixfmt default.nix"
          exit 1
        }
        touch $out
      '';
    });
    hunspellCheck = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-hunspell";
      buildInputs = baseAttrs.buildInputs
      ++ [(hunspellWithDicts (with hunspellDicts; [en-us]))];
      buildPhase = ''
        # Required by =sort= and =hunspell=
        export LANG=C.UTF-8

        for DICT in spelling/*.txt; do
          diff <(sort $DICT) $DICT || {
            echo "The $DICT dictionary is unsorted. To fix it, run:"
            echo "  LANG=C.UTF-8 sort $DICT | sponge $DICT"
            exit 1
          }
        done

        HTML_DIR="${subtasks.docs}"
         hunspell -l -p <(cat spelling/international.dic.txt spelling/en_US.dic.txt spelling/en_US.aff.txt) -d en_US -i utf-8 $(find "$HTML_DIR" -type f -name '*.html') | tee spelling.txt
        [[ -s spelling.txt ]] && {
          echo "Mispelled words detected by hunspell."
          exit 1
        }

        touch $out
      '';
    });
    docs = baseTask.overrideAttrs (baseAttrs: {
      name = "${baseAttrs.name}-docs";
      src = ./site;
      buildInputs = [ websiteBuilder pandoc ];
      buildPhase = ''
        export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive";
        export LANG=en_US.UTF-8

        ls pastebin/*.org | awk -F. '{print $1}' | xargs -I{} pandoc -o {}.html {}.org --standalone

        build-site build

        mkdir $out
        cp -r _site/* $out
      '';
    });
  };
  test = baseTask.overrideAttrs (baseAttrs: rec {
    name = "${baseAttrs.name}-test";
    buildInputs = [
      subtasks.fixme
      subtasks.perlPodCheck
      subtasks.batsTest
      subtasks.perlInternalTest
      subtasks.formatNix
      subtasks.hunspellCheck
    ];
    buildPhase = ''
      echo "Ran tests for:"
      for d in ${builtins.toString buildInputs}; do
        echo "  $d"
      done
      echo "All tests passed!"
      touch $out
    '';
  });
  shell = mkShell rec {
    name = "website-shell";
    buildInputs = [
      websiteBuilder
      (pkgs.haskellPackages.ghcWithPackages (p: with p; [hakyll]))
      nixfmt
      (hunspellWithDicts (with hunspellDicts; [en-us]))
    ];
  };
  publishScript = pkgs.writeShellScriptBin "publish.sh" ''
    set -euo pipefail
    ${pkgs.rsync}/bin/rsync --verbose                               \
                            --copy-links                            \
                            --progress                              \
                            --stats                                 \
                            --update                                \
                            --recursive                             \
                            --rsh="ssh -o StrictHostKeyChecking=no" \
                            ${subtasks.docs}/                       \
                            "$SERVER_URL:$DOCS_SERVER_PATH"
  '';
}