One way to achieve that is as follows:
function build_model(flag::Bool)
model = Model()
@variable(model, x)
if flag
@constraint(model, x >= 0)
end
@objective(model, Min, x)
return model
end
Projects like PowerModels.jl very successfully use this approach to automatically build JuMP models that depend on the context.