FreeBSD for Developer

Posted on Jul 5, 2024
The plan is to set up FreeBSD 14.1 on a NUC for development. The main languages used are C/C++, Python, and Rust. The environment is freebsd-based, focusing on cross-compilation and static compilation.

Environment

Platform:

  • NUC12WSBi5

Development Toolkit:

  • FreeBSD 14.1-RELEASE
  • clang 18.1.5 (C/C++)
  • python 3.11 (Python)

Install tools:

  • sudo pkg install emacs bash sudo htop tree googletest wget curl git xauth cpufetch translate-shell x11-fonts/dejavu

OpenBSD Secure Shell

/etc/ssh/sshd_config

...
PermitRootLogin yes
PermitEmptyPasswords yes
X11Forwarding yes
X11DisplayOffset 10

service sshd enable && service sshd restart

Coredump

/etc/profile

ulimit -c unlimited

/etc/sysctl.conf

# corefile
kern.corefile=/var/crash/core.%P

Python Development

pkg install python311

Rust Development

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

C/C++ Development

pkg install llvm18 automake cmake

Pre-installed by default:

  • clang/clangd
  • make

Debug Tools

pkg install valgrind

Pre-installed by default:

  • lldb
  • truss
  • dtrace
  • gdb/cgdb

Neovim

# install neovim
sudo pkg install neovim nodejs npm xclip

# vim-plug manager
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

~/.config/nvim/init.vim

" Plug manager
call plug#begin('~/.local/share/nvim/plugged')
  Plug 'folke/tokyonight.nvim'
  Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()

" Use coc-clangd
let g:coc_global_extensions = ['coc-python', 'coc-clangd', 'coc-json']

" CoC config file
let g:coc_config_home = expand('~/.config/nvim')

" clangd
let g:coc_clangd_path = '/usr/local/bin/clangd'

" Theme
colorscheme tokyonight

" Set tab width to 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab

" Show line numbers
set number

" Configure wildmenu completion behavior
set wildmode=full

" Set text width to 120 columns for code style
set cc=120

" Highlight the current line where the cursor is
set cursorline

" Mouse support
set mouse=a

" clipboard
set clipboard+=unnamedplus

~/.config/nvim/coc-settings.json

{
  "clangd.enabled": true,
  "clangd.path": "/usr/local/bin/clangd17",
    "clangd.arguments": [
    "--background-index",
    "--clang-tidy",
    "-j=2"
  ],
  "suggest.autoTrigger": "always"
}
# Install plug
:PlugInstall

Git

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.cm commit
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cy cherry

git config --global user.name "user"
git config --global user.email "<[email protected]>"
git config --global core.editor vim

# git config --global http.proxy 'socks5://127.0.0.1:1080'
# git config --global https.proxy 'socks5://127.0.0.1:1080'
# git config --global --unset http.proxy
# git config --global --unset https.proxy
# git config http.proxy 'socks5://127.0.0.1:1080'
# git config https.proxy 'socks5://127.0.0.1:1080'
# git config --unset http.proxy
# git config --unset https.proxy

Jails

# install ezjail
sudo pkg install ezjail
sudo ezjail-admin install

# create Jail
sudo ezjail-admin create -r /jails/devbox devbox 'lo0|127.0.0.1'

# config Jail
sudo sh -c 'cat <<EOF > /usr/local/etc/ezjail/devbox
export jail_devbox_hostname="devbox"
export jail_devbox_ip="lo0|127.0.0.1"
export jail_devbox_rootdir="/jails/devbox"
export jail_devbox_exec_start="/bin/sh /etc/rc"
export jail_devbox_exec_stop="/bin/sh /etc/rc.shutdown"
export jail_devbox_mount_enable="YES"
export jail_devbox_devfs_enable="YES"
export jail_devbox_devfs_ruleset="devfsrules_jail"
EOF'

# start Jail
sudo ezjail-admin start devbox
# stop Jail
sudo ezjail-admin stop devbox
# delete Jail
sudo ezjail-admin delete -w devbox
# list Jails
jls
# enter Jails
sudo ezjail-admin console devbox