@@@@@@@@  @@@@@@@   @@@@@@@@  @@@@@@@@  @@@@@@@    @@@@@@   @@@@@@@
      @@@@@@@@  @@@@@@@@  @@@@@@@@  @@@@@@@@  @@@@@@@@  @@@@@@@   @@@@@@@@
      @@!       @@!  @@@  @@!       @@!       @@!  @@@  !@@       @@!  @@@
      !@!       !@!  @!@  !@!       !@!       !@   @!@  !@!       !@!  @!@
      @!!!:!    @!@!!@!   @!!!:!    @!!!:!    @!@!@!@   !!@@!!    @!@  !@!
      !!!!!:    !!@!@!    !!!!!:    !!!!!:    !!!@!!!!   !!@!!!   !@!  !!!
      !!:       !!: :!!   !!:       !!:       !!:  !!!       !:!  !!:  !!!
      :!:       :!:  !:!  :!:       :!:       :!:  !:!      !:!   :!:  !:!
       ::       ::   :::   :: ::::   :: ::::   :: ::::  :::: ::    :::: ::
       :         :   : :  : :: ::   : :: ::   :: : ::   :: : :    :: :  :

Based on FreeBSD 11.1

 ____ 
/_   |     Post-base system installation steps
 |   |    ______________________________________________________________________
 |   |
 |___|

All these commands should be run as root.

These steps are cherry-picked from Allison Reid's excellent FreeBSD guide,
Stackoverflow, and the FreeBSD forums.

    $ freebsd-update fetch
    $ freebsd-update install
    $ pkg install xorg

Reboot.

Edit /etc/login.conf. The "default" configuration block ends in \:umask=022:
Change that line and add a couple underneath it:

    \:umask=022:\
    \:charset=UTF-8:\
    \:lang=en_US.UTF-8:

Edit /etc/profile and add:

    LANG=en_US.UTF-8;
    CHARSET=UTF-8;

That sets the LANG and CHARSET environment variables system-wide. These can be
overridden in user-specific profiles.

FreeBSD is still primarily used as a server operating system. There are some
settings we can tweak for better performance given the desktop use-case.

Edit /etc/sysctl.conf and add:

    # Increase shared memory limits for xorg
    kern.ipc.shmmax=67108864
    kern.ipc.shmall=32768

    # Make UI more responsive under high CPU load
    # https://www.mail-archive.com/[email protected]/msg112118.html
    kern.sched.preempt_thresh=224

Edit /boot/loader.conf and add:

    # Shared memory, max process increases
    kern.ipc.shmseg=1024
    kern.ipc.shmmni=1024
    kern.maxproc=100000

    # Support filesystems in user space
    fuse_load="YES"

    # Thermal sensors for Intel chips
    # Change to amdtemp_load="YES" for amd chips
    coretemp_load="YES"

    # Enable temporary filesystems
    # Required by certain applications, but can leave
    # disabled until explicitly needed
    tmpfs_load="YES"

    # Asynchronous I/O
    aio_load="YES"

Reboot.

________  
\_____  \  nVidia driver configuration (optional)
 /  ____/ ______________________________________________________________________
/       \ 
\_______ \
        \/

    $ pkg install x11/nvidia-driver
        * Depending on what card you're using, you may need the
          x11/nvidia-driver-340 or x11/nvidia-driver-304 instead. Check
          this page to determine which driver is for your card.
    $ sysrc kld_list="nvidia"
        * If you installed driver version 358.009 or newer:
        $ sysrc kld_list+="nvidia-modeset"

Reboot.

    $ mkdir -p /usr/local/etc/X11/xorg.conf.d
    $ {nano,vim,ed} /usr/local/etc/X11/xorg.conf.d/driver-nvidia.conf:

    Section "Device"
        Identifier "NVIDIA Card"
        VendorName "NVIDIA Corporation"
        Driver "nvidia"
    EndSection

Reboot.

________  
\_____  \  The Common Desktop Environment
  _(__  < ______________________________________________________________________
 /       \
/______  /
       \/ 

We need to set a fully-qualified domain name for the system. Failing to do this
properly will prevent CDE from launching.

    $ hostname -f
        * Returns something like lain.my.domain

Edit /etc/rc.conf and look for hostname=lain.my.domain. It should
already be set here. If it is not, set it, making sure it matches the output
from above.

Edit the line of /etc/hosts beginning with 127.0.01 to look like:

    127.0.0.1   lain.my.domain lain localhost localhost.localdomain

Note that regardless of your hostname, localhost localhost.localdomain will
always be the same.

Next we need to install some required packages:

    $ pkg install xorg git iconv bdftopcf libXp libXScrnSaver ksh93 open-motif

Edit /etc/rc.conf and add the lines:

    rpcbind_enable="YES"
    inetd_enable="YES"

Reboot for good measure.

To install CDE:

    $ git clone git://git.code.sf.net/p/cdesktopenv/code cdesktopenv-code
    $ cd cdesktopenv-code/cde
    $ mkdir -p imports/x11/include
    $ ln -s /usr/local/include/X11 imports/x11/include
    $ ln -s /usr/local/include/Xm imports/x11/include
    $ ln -s /usr/local/include/fontconfig imports/x11/include
    $ ln -s /usr/local/include/freetype2 imports/x11/include
    $ make World
    $ cd cdesktopenv-code/cde/admin/IntegTools/dbTools
    $ ./installCDE -s ../../../../cde

Enable ToolTalk, CDE's inter-process messaging service:

    $ mkdir -p /etc/tt
    $ cp cdesktopenv-code/cde/programs/tttypes/types.xdr /etc/tt

Finally, create /etc/rc.local with the content:

    /usr/dt/bin/dtlogin

That enables CDE's display manager. Reboot.

By default, CDE does not source ~/.profile or /etc/login.conf - instead it uses
~/.dtprofile. Uncomment this line in .dtprofile:

    # DTSOURCEPROFILE=true

This solves the issue of some programs not being found.

   _____  
  /  |  |   Miscellaneous
 /   |  |_ _____________________________________________________________________
/    ^   /
\____   | 
     |__| 

CDE uses the .pm file format for bitmap graphics. An easy way to convert
existing icons for applications from other formats is to use ImageMagick's
convert command:

    $ convert file.png icon.xpm
    $ convert icon.xpm -resize 48x48 icon.pm

For further reading:
    * The aforementioned Allison Reid's excellent FreeBSD guide. It contains
      additional configuration steps specific to laptops.
    * FreeBSD uses OpenBSD's pf firewall. It is incredibly powerful and not
      too complex but is beyond the scope of these notes. See OpenBSD's
      documentation on pf, or a book on the subject.
    * The tuning manpage on FreeBSD.
    * The SystemTuning wiki page. It is "initially just the current tuning(7)
      manpage with some annotations" but does have some additional info and
      explanations.